Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Discrete Fourier transform

Tags:

c#

math

fft

dft

I am currently trying to write some fourier transform algorithm. I started with a simple DFT algorithm as described in the mathematical definition:

public class DFT {     public static Complex[] Transform(Complex[] input) {         int N = input.Length;          Complex[] output = new Complex[N];          double arg = -2.0 * Math.PI / (double)N;         for (int n = 0; n < N; n++) {             output[n] = new Complex();             for (int k = 0; k < N; k++)                 output[n] += input[k] * Complex.Polar(1, arg * (double)n * (double)k);         }         return output;     } } 

So I tested this algorithm with the following code:

    private int samplingFrequency = 120;     private int numberValues = 240;      private void doCalc(object sender, EventArgs e) {         Complex[] input = new Complex[numberValues];         Complex[] output = new Complex[numberValues];          double t = 0;         double y = 0;         for (int i = 0; i < numberValues; i++) {             t = (double)i / (double)samplingFrequency;             y = Math.Sin(2 * Math.PI * t);             input[i] = new Complex(y, 0);         }          output = DFT.Transform(input);          printFunc(input);         printAbs(output);     } 

The transformation works fine, but only if numberValues is a multiple number of the samplingFrequency (in this case: 120, 240, 360,...). Thats my result for 240 values:

The transformation just worked fine.

If i am trying to calculate 280 values I get this result:

Why I am getting a incorrect result if I change the number of my calculated values? I am not sure if my problem here is a problem with my code or a misunderstanding of the mathematical definition of the DFT. In either way, can anybody help me with my problem? Thanks.

like image 796
Marcel Avatar asked Sep 28 '11 11:09

Marcel


People also ask

What is Discrete Fourier Transform?

In mathematics, the discrete Fourier transform (DFT) converts a finite sequence of equally-spaced samples of a function into a same-length sequence of equally-spaced samples of the discrete-time Fourier transform (DTFT), which is a complex-valued function of frequency.

What is Discrete Fourier Transform used for?

The Discrete Fourier Transform (DFT) is of paramount importance in all areas of digital signal processing. It is used to derive a frequency-domain (spectral) representation of the signal.

What is DFT and FFT?

A fast Fourier transform (FFT) is an algorithm that computes the discrete Fourier transform (DFT) of a sequence, or its inverse (IDFT). Fourier analysis converts a signal from its original domain (often time or space) to a representation in the frequency domain and vice versa.

What is Discrete Fourier Transform in DSP?

< Digital Signal Processing. Digital Signal Processing. As the name implies, the Discrete Fourier Transform (DFT) is purely discrete: discrete-time data sets are converted into a discrete-frequency representation. This is in contrast to the DTFT that uses discrete time, but converts to continuous frequency.


1 Answers

What you are experiencing is called Spectral Leakage.

This is caused because the underlying mathematics of the Fourier transform assumes a continuous function from -infinity to + infinity. So the range of samples you provide is effectively repeated an infinite number of times. If you don't have a complete number of cycles of the waveform in the window the ends won't line up and you will get a discontinuity which manifests its self as the frequency smearing out to either side.

The normal way to handle this is called Windowing. However, this does come with a downside as it causes the amplitudes to be slightly off. This is the process of multiply the whole window of samples you are going to process by some function which tends towards 0 at both ends of the window causing the ends to line up but with some amplitude distortion because this process lowers the total signal power.

So to summarise there is no error in your code, and the result is as expected. The artefacts can be reduced using a window function, however this will effect the accuracy of the amplitudes. You will need to investigate and determine what solution best fits the requirements of your project.

like image 194
Charles Keepax Avatar answered Sep 19 '22 20:09

Charles Keepax