DSPRelated.com
Forums

Spectral Analysis using MATLAB

Started by fara...@gmail.com August 18, 2006
Hi everyone,
I am new with matlab and am having problems with a spectral analysis
problem i'm trying to do. I need to perform spectral analysis of a
signal consisting of 1000Hz, 1000.1Hz and 999.9Hz i.e.,
x=cos(2*pi*1000*t)+cos(2*pi*1000.1*t)+cos(2*pi*999.9*t) so that all the
three peaks are seen. This is to be done using a windowing function
like blackmann-harris/guassian.

I wrote the m-file to obtain peak for only one frequency. I'm totally
stumned with how to obtained all three frequency peaks. Here's my
m-file I'm working on. Any ideas on what to choose for the value of L,
N and Fs ???? Any help would be great, since i've no idea on how to
proceed. The m-file below gives me a peak at 999.9 Hz only and nothing
at 1000 and 1000.1 Hz.

Fs = 5000;  % sampling frequency
Ts = 1/Fs;  %  sampling period
L = 25;      % number of samples
N = 1024*4;   %  number of points for FFT
f1 = 1000     %  analog frquency in Hz
f2=1000.1
f3=999.9
A1 = 1.7;

t = (0:L-1)*Ts;     %  array of sampled time from 0 to Ts*(L-1)
x = A1*cos(2*pi*f1*t)+A1*cos(2*pi*f2*t)+A1*cos(2*pi*f3*t);  %  sampled
signal
X = fft(x,N); % take DFT of padded signal
XX = fftshift(X)   %center spectrum
bin_inc = Fs/N,     %compute the increment between discrete frequency
bins
f =[-N/2:(N/2-1)]*bin_inc;  %  create an array of frequencies
figure
plot(f, abs(XX));   %  plot the magnitude annotated with frequency
title('Rectangular')
xlabel('frequency Hz')
ylabel('magnitude')
zzR = max(abs(XX))
ampR = zzR/N*2

%  now use windows to process the data prior to performing FFT
wb  = window(@blackmanharris,L);
xb = x.*wb'; %  blackman window
XB =fftshift(fft(xb,N));

%  plot FFT
figure
plot(f, abs(XB))
title('Blackman-Harris');
xlabel('frequency Hz')
ylabel('magnitude')
zzB = max(abs(XB))
ampB = zzB/N*2

Thanks a lot in advance, waiting for reply. 
-Farah

ampspectrum=abs(fft(your_time_sequence,2*the_length_of_your_time_sequence))

if N is the length of your sequence and your sample freq. is f then

f[n]=((fs/2)*(n-1))/N

where n is between 1 and N
-------


<farah727rash@gmail.com> skrev i en meddelelse 
news:1155903928.266355.12150@i3g2000cwc.googlegroups.com...
> Hi everyone, > I am new with matlab and am having problems with a spectral analysis > problem i'm trying to do. I need to perform spectral analysis of a > signal consisting of 1000Hz, 1000.1Hz and 999.9Hz i.e., > x=cos(2*pi*1000*t)+cos(2*pi*1000.1*t)+cos(2*pi*999.9*t) so that all the > three peaks are seen. This is to be done using a windowing function > like blackmann-harris/guassian. > > I wrote the m-file to obtain peak for only one frequency. I'm totally > stumned with how to obtained all three frequency peaks. Here's my > m-file I'm working on. Any ideas on what to choose for the value of L, > N and Fs ???? Any help would be great, since i've no idea on how to > proceed. The m-file below gives me a peak at 999.9 Hz only and nothing > at 1000 and 1000.1 Hz. > > Fs = 5000; % sampling frequency > Ts = 1/Fs; % sampling period > L = 25; % number of samples > N = 1024*4; % number of points for FFT > f1 = 1000 % analog frquency in Hz > f2=1000.1 > f3=999.9 > A1 = 1.7; > > t = (0:L-1)*Ts; % array of sampled time from 0 to Ts*(L-1) > x = A1*cos(2*pi*f1*t)+A1*cos(2*pi*f2*t)+A1*cos(2*pi*f3*t); % sampled > signal > X = fft(x,N); % take DFT of padded signal > XX = fftshift(X) %center spectrum > bin_inc = Fs/N, %compute the increment between discrete frequency > bins > f =[-N/2:(N/2-1)]*bin_inc; % create an array of frequencies > figure > plot(f, abs(XX)); % plot the magnitude annotated with frequency > title('Rectangular') > xlabel('frequency Hz') > ylabel('magnitude') > zzR = max(abs(XX)) > ampR = zzR/N*2 > > % now use windows to process the data prior to performing FFT > wb = window(@blackmanharris,L); > xb = x.*wb'; % blackman window > XB =fftshift(fft(xb,N)); > > % plot FFT > figure > plot(f, abs(XB)) > title('Blackman-Harris'); > xlabel('frequency Hz') > ylabel('magnitude') > zzB = max(abs(XB)) > ampB = zzB/N*2 > > Thanks a lot in advance, waiting for reply. > -Farah >
You are not using enough points for the required resolution.
You have the answer on your code:

bin_inc = Fs/N,     %compute the increment between discrete frequency


if bin_inc is set to 0.1 Hz than the number of points should be

bin_inc=0.1;
alpha=1.5;
L = alpha*Fs/bin_inc;      % number of samples

Where alpha is just a constant to give a slightly bigger number of
poinst (because the FFT bins might not be *exactly* centered in the
frequencies you want to detect.

This is the case for the rectangular window, which has the best
resolution. For the blackman window you will have to increase the
number of points even further... (ie, alpha = 2.5).


Obs: You can reduce the sampling frequency to save on the number of
points (ie Fs=3 kHz).  Also , make sure you zoom in the plot since the
peaks are very close together, but you are plotting the whole spectral
range.

You are not using enough points for the required resolution.
You have the answer on your code:

bin_inc = Fs/N,     %compute the increment between discrete frequency


if bin_inc is set to 0.1 Hz than the number of points should be


bin_inc=0.1;
alpha=1.5;
L = alpha*Fs/bin_inc;      % number of samples
N=L;

Where alpha is just a constant to give a slightly bigger number of
poinst (because the FFT bins might not be *exactly* centered in the
frequencies you want to detect.


This is the case for the rectangular window, which has the best
resolution. For the blackman window you will have to increase the
number of points even further... (ie, alpha = 2.5).


Obs: You can reduce the sampling frequency to save on the number of
points (ie Fs=3 kHz).  Also , make sure you zoom in the plot since the
peaks are very close together, but you are plotting the whole spectral
range.

Hey lkaro, thanks a lot. Though i did not use alpha for this part, i
just set Fs around 2020Hz and bin_inc as 0.05 since separation between
the frequencies is 0.1 using which i calculated N and L. The problem
later i was having was that i forgot the zooming in part cos of which i
wasnt able to see the discrete peaks clearly.  I am thinkning of just
plotting the frequencies in the range of -1010:1010 which would save me
having to zoom in. thanks for your help.


Ikaro wrote:
> You are not using enough points for the required resolution. > You have the answer on your code: > > bin_inc = Fs/N, %compute the increment between discrete frequency > > > if bin_inc is set to 0.1 Hz than the number of points should be > > > bin_inc=0.1; > alpha=1.5; > L = alpha*Fs/bin_inc; % number of samples > N=L; > > Where alpha is just a constant to give a slightly bigger number of > poinst (because the FFT bins might not be *exactly* centered in the > frequencies you want to detect. > > > This is the case for the rectangular window, which has the best > resolution. For the blackman window you will have to increase the > number of points even further... (ie, alpha = 2.5). > > > Obs: You can reduce the sampling frequency to save on the number of > points (ie Fs=3 kHz). Also , make sure you zoom in the plot since the > peaks are very close together, but you are plotting the whole spectral > range.