Problem with Matlab code... FFT Analysis

edited March 2007 in Science & Tech
Hi,

I know this is a really basic question, but I just can't figure out why my simple matlab code below isn't working!

I'm playing with FFT analysis and to run some tests I want to generate a sampled data set that is constructed from every fundamental frequency of each FFT bin.

I'm emulating sampled audio at 44.1K and want to generate a 2048 sample window.

I have defined the following variables:

fs = 44100; % sampling rate
sp = 1/fs; % sampling period
ns = 2048; % number of samples
br = fs/ns; % fft bin resolution = 21.5332 Hz

Therefore each FFT bin will center at multiples of 21.5332 Hz.

To generate the wave I'm using the loop below, but the resulting waveform has a nasty spikes at the beginning and end of the sample window:


y = zeros(1,ns);
% generate sample made up of the FFT bin centre ffrequencies
for i = br:br:fs/2,
y = y + sin(2*pi*i*[0: sp: ((ns-1)*sp) ]);
end

I've attached a plot of (y). What am I doing wrong????

Thanks,
Vince.
<!-- / message --><!-- attachments -->

Comments

  • shwaipshwaip bluffin' with my muffin Icrontian
    edited March 2007
    the definition of the DTFT is:
    [php]

    . N-1
    X[k] = sum(x[n]*exp(2*pi*j*n*k/N))
    n=0
    [/php]
    (j = sqrt(-1))

    Your definition assumes your function is purely odd, and is imaginary. You're also only evaluating over the first half of the fourier frequencies. For purely real functions, this is fine, but yours isn't a purely real function.

    Remember, when you implement it like above, you'll end up with positive low frequencies near k=0, high frequencies near k=N/2, negative low frequencies near k=N-1
  • edited March 2007
    Hi,

    Thanks for your reply.

    My question was not with the FFT part of my task, just on the generation of sampled data to feed into an FFT.

    Are you saying that I have incorrectly calculated the center frequencies for the FFT bins from 1 to fs/2?

    In either case I just didn't expect the sum of the sine waves generated in the for loop to look like the plot shown in the image.

    Can you confirm that even if I'm generating the wrong frequencies, that the method used to add them together is correct?

    Thanks,
    Vince.



    shwaip wrote:
    the definition of the DTFT is:
    [php]

    . N-1
    X[k] = sum(x[n]*exp(2*pi*j*n*k/N))
    n=0
    [/php]
    (j = sqrt(-1))

    Your definition assumes your function is purely odd, and is imaginary. You're also only evaluating over the first half of the fourier frequencies. For purely real functions, this is fine, but yours isn't a purely real function.

    Remember, when you implement it like above, you'll end up with positive low frequencies near k=0, high frequencies near k=N/2, negative low frequencies near k=N-1
  • shwaipshwaip bluffin' with my muffin Icrontian
    edited March 2007
    Well...you're calculating what you're claiming to be the FFT over a series of ones of length N.

    But it looks to me like you're summing over frequency (i=br:br:fs/2), where you should be summing over n (n=0:N-1). They may be equivalent, but I've _never_ seen that version.

    edit:

    Test it on a 1 point sequence:

    DFT({1}) = 1
    yourFFT({1}) = 0
  • edited March 2007
    What I'm trying to achieve with the summation is a time domain waveform that is made up of all the frequencies that I can extract from the real portion of the FFT result.

    Here is my understanding, please correct me if I've got it all wrong!


    As the sampling frequency is 44.1K, therefore the highest frequency I can include in the time domain is fs/2 = 22.05K

    As there are 2048 samples, then the center frequency of adjacent FFT bins will be separated by fs/2048 = 21.5332 Hz.

    Meaning I will generate a time domain data set, 2048 samples in length, containing 1024 sine waves, each with a frequency that corresponds to a center frequency of one of the 1st 1024 FFT bins, each one spaced by 21.5332 Hz.

    The time domain waveform shown in the plot is not what I expected the summation of these waves to look like. Note the plot is time domain, not frequency.

    Vince.




    shwaip wrote:
    Well...you're calculating what you're claiming to be the FFT over a series of ones of length N.

    But it looks to me like you're summing over frequency (i=br:br:fs/2), where you should be summing over n (n=0:N-1). They may be equivalent, but I've _never_ seen that version.
  • shwaipshwaip bluffin' with my muffin Icrontian
    edited March 2007
    That's what it should look like, then.

    If you're unsure, try just plotting the i=1 term, then the sum from i=1:2, i=1:4...and so on, until you're sure that you're seeing what you want.
Sign In or Register to comment.