【发布时间】:2012-10-05 03:35:18
【问题描述】:
我正在尝试为我在学校做的一个项目实施离散傅立叶变换算法。但是创建一个类似乎很困难(不应该如此)。 我正在使用 Visual Studio 2012。
基本上我需要一个名为 Complex 的类来存储我从 DFT 获得的两个值;实部和虚部。
这就是我目前所拥有的:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SoundEditor_V3
{
public class Complex
{
public double real;
public double im;
public Complex()
{
real = 0;
im = 0;
}
}
}
问题是它不承认构造函数是构造函数,我只是在学习C#,但是我在网上查了一下,它应该是这样的。 但它将我的构造函数识别为方法。
这是为什么呢? 我创建的课程有误吗?
我的傅立叶课也是如此。所以每次我尝试创建一个 傅里叶对象,然后使用它的方法......没有这样的事情。
例如,我这样做:
Fourier fou = new Fourier();
fou.DFT(s, N, amp, 0);
它告诉我 fou 是一个“字段”,但用作“类型” 为什么这么说?
这也是我的傅立叶类的代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SoundEditor_V3
{
public class Fourier
{
//FOURIER
//N = number of samples
//s is the array of samples(data)
//amp is the array where the complex result will be written to
//start is the where in the array to start
public void DFT(byte[] s, int N, ref Complex[] amp, int start)
{
Complex tem = new Complex();
int f;
int t;
for (f = 0; f < N; f++)
{
tem.real = 0;
tem.im = 0;
for (t = 0; t < N; t++)
{
tem.real += s[t + start] * Math.Cos(2 * Math.PI * t * f / N);
tem.im -= s[t + start] * Math.Sin(2 * Math.PI * t * f / N);
}
amp[f].real = tem.real;
amp[f].im = tem.im;
}
}
//INVERSE FOURIER
public void IDFT(Complex[] A, ref int[] s)
{
int N = A.Length;
int t, f;
double result;
for (t = 0; t < N; t++)
{
result = 0;
for (f = 0; f < N; f++)
{
result += A[f].real * Math.Cos(2 * Math.PI * t * f / N) - A[f].im * Math.Sin(2 * Math.PI * t * f / N);
}
s[t] = (int)Math.Round(result);
}
}
}
}
我现在非常困惑,任何和所有的帮助都将不胜感激。谢谢。
编辑:
这是我尝试访问所有课程的地方:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace SoundEditor_V3
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
string filename;
NAudio.Wave.WaveStream waveStream;
private NAudio.Wave.DirectSoundOut sout = null;
private void openToolStripMenuItem_Click(object sender, EventArgs e)
{
OpenFileDialog open = new OpenFileDialog();
open.Filter = "Wave File (*.wav)|*.wav";
if (open.ShowDialog() != DialogResult.OK)
{
return;
}
waveStream = new NAudio.Wave.WaveFileReader(open.FileName);
filename = open.FileName;
sout = new NAudio.Wave.DirectSoundOut();
sout.Init(new NAudio.Wave.WaveChannel32(waveStream));
}
//Play
private void Play_btn_Click(object sender, EventArgs e)
{
sout.Play();
}
//Stop
private void Stop_btn_Click(object sender, EventArgs e)
{
sout.Stop();
waveStream.Position = 0;
}
//Pause
private void Pause_btn_Click(object sender, EventArgs e)
{
sout.Pause();
}
//display fourier
//N = number of samples(length of array)
//s is the array of samples(data)
//amp is the array where the complex result will be written to
//start is the where in the array to start
static int N = 8;
byte[] s = {1,2,3,4,5,6,7,8};
Complex[] amp = new Complex[N];
Fourier xfo = new Fourier();
//xfo.DFT(s, N, amp, 0);
}
}
【问题讨论】:
-
向我们展示amp的声明
-
另外,这可能看起来很奇怪,但是发布整个文件以供您使用类而不是这两行。文件中有一些非常愚蠢的拼写错误会导致这些错误。
-
这些行是一个方法吗?如果您尝试在方法之外的类主体中编写该代码,则可能会出现该错误。
-
并确保在传递 amp 参数时使用 ref 关键字
-
这是我对 amp 的声明,虽然很可能是错误的。
Complex[] amp = new Complex[N];
标签: c# class constructor visual-studio-2012