To talk on Icrontic, just register!

It only takes 30 seconds.

Have an account? Sign in:

Forgot?
vince365
New to the neighborhood
vince365
3 Posts

Problem with Matlab code... FFT Analysis

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.
Attached Thumbnails
Click image for larger version

Name:	matlab_plot.jpg
Views:	80
Size:	85.2 KB
ID:	22725  
shwaip
elaborate bot
shwaip
5,730 Posts

Re: Problem with Matlab code...

the definition of the DTFT is:
PHP Code:
 
.      N-1
X
[k] = sum(x[n]*exp(2*pi*j*n*k/N))
       
n=
(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
__________________ my photostream for ic photography challenge

Anyone who wants dropbox, please use my referral link
vince365
New to the neighborhood
vince365
3 Posts

Re: Problem with Matlab code...

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.




the definition of the DTFT is:
PHP Code:
 
.      N-1
X
[k] = sum(x[n]*exp(2*pi*j*n*k/N))
       
n=
(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
shwaip
elaborate bot
shwaip
5,730 Posts

Re: Problem with Matlab code...

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
vince365
New to the neighborhood
vince365
3 Posts

Re: Problem with Matlab code...

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.





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.
shwaip
elaborate bot
shwaip
5,730 Posts

Re: Problem with Matlab code...

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.
Similar Threads
Thread Thread Starter Forum Replies Last Post
A unique problem not my own. :x Drax Resolved / Inactive 1 26 Dec 2004 5:55am
Matlab help!!! pseudonym Web & Digital Media 11 11 Dec 2004 8:44pm
Quotation Mark (Key) Problem vanagon40 Operating Systems 11 18 Jun 2004 4:40pm
Windows NT 4 & 2000 Source Code leaked SM-Bot Technology Articles 17 15 Feb 2004 4:34am
China looks into Windows code Spinner Technology Articles 4 30 Sep 2003 6:25am

Go Back   Icrontic Forums > Tech: Software > General Software > Matlab Help
Jump to
This Thread Search this Thread
Search this Thread:

Advanced Search


Current time: 4:22pm (GMT)
Powered by vBulletin®
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Get Vanilla instead. Trust me.