# Problem generating sine waves in Matlab

Discussion in 'Programmer's Corner' started by vince365, Mar 2, 2007.

1. ### vince365 Thread Starter New Member

Mar 2, 2007
1
0
Hi,

I know this is a really basic question to post here, 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.

File size:
85.2 KB
Views:
36
2. ### Murod Active Member

Dec 24, 2005
30
0
what do you mean with "fundamental frequency of each FFT bin"?

3. ### Dave Retired Moderator

Nov 17, 2003
6,960
145
For a given sample the number of FFT points gives the frequency resolution, that is frequencies represented by a single bar. An FFT bar is referred to as a FFT bin when you want to characterise the energy/voltage from a frequency range, not a single frequency. This frequency range has a fundamental frequency.

Dave