我正在尝试为我在学校做的一个项目实施离散傅立叶变换算法。但是创建一个类似乎很困难(不应该如此)。我正在使用 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);
}
}