Difficulty creating classes and arrays of those classes C#
Posted
by
Lucifer Fayte
on Stack Overflow
See other posts from Stack Overflow
or by Lucifer Fayte
Published on 2012-10-05T03:35:18Z
Indexed on
2012/10/05
3:37 UTC
Read the original article
Hit count: 214
I'm trying to implement a Discrete Fourier Transformation algorithm for a project I'm doing in school. But creating a class is seeming to be difficult(which it shouldn't be). I'm using Visual Studio 2012.
Basically I need a class called Complex to store the two values I get from a DFT; The real portion and the imaginary portion.
This is what I have so far for that:
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;
}
}
}
The problem is that it doesn't recognize the constructor as a constructor, now I'm just learning C#, but I looked it up online and this is how it's supposed to look apparently. It recognizes my constructor as a method.
Why is that? Am I creating the class wrong?
It's doing the same thing for my Fourier class as well. So each time I try to create a Fourier object and then use it's method...there is no such thing.
example, I do this:
Fourier fou = new Fourier();
fou.DFT(s, N, amp, 0);
and it tells me fou is a 'field' but is used like a 'type' why is it saying that?
Here is the code for my Fourier class as well:
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);
}
}
}
}
I'm very much stuck at the moment, any and all help would be appreciated. Thank you.
© Stack Overflow or respective owner