Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

spectrum analysis with the fft basics and Matlab 4 begineers

Status
Not open for further replies.

rramya

Member level 4
Joined
Dec 14, 2008
Messages
76
Helped
23
Reputation
46
Reaction score
4
Trophy points
1,288
Activity points
2,062
fft basics

hi every one,
i would like to share some of the uses of fft for spectrum analysis.
Hope it will be useful for those who are novice to MATLAB programming.

DFT Notes:
DFT produces a discrete frequency domain representation.Also, DFT is only defined
in the region between 0 and Fs.
(as we know, One period extends from f = 0 to Fs, where Fs is the sampling frequency.)
When the region between 0 and Fs is examined, it can be seen that there is even
symmetry around the center point, 0:5Fs, the Nyquist frequency.
This symmetry adds redundant information.(the data between 0:5fs and fs is a mirror
image of the data between 0 and 0:5Fs.)
Matlab's FFT function is an effective tool for computing the discrete Fourier transform
of a signal. The typical syntax for computing the FFT of a signal is fft(x,N) where x is the
signal, x[n], you wish to transform, and N is the number of points in the FFT.
N must be at least as large as the number of samples in x[n].

Example:1
To demonstrate the effect of changing the value of N,
sythesize a cosine with 30 samples at 10 samples per period.
n = [0];
x = cos(2*pi*n/10);
Define 3 different values for N.
The abs function in MATLAB finds the magnitude of the transform,
N1 = 64;
N2 = 128;
N3 = 256;
X1 = abs(fft(x,N1));
X2 = abs(fft(x,N2));
X3 = abs(fft(x,N3));
to plot the magnitude of the transform we need to scale the freq.
The frequency scale begins at 0 and extends to N-1 for an N-point FFT.
We then normalize the scale so that it extends from 0 to [ 1-(1/N) ] .
F1 = [0 : N1 - 1]/N1;
F2 = [0 : N2 - 1]/N2;
F3 = [0 : N3 - 1]/N3;
subplot(3,1,1)
plot(F1,X1,'-x'),title('N = 64'),axis([0 1 0 20])
subplot(3,1,2)
plot(F2,X2,'-x'),title('N = 128'),axis([0 1 0 20])
subplot(3,1,3)
plot(F3,X3,'-x'),title('N = 256'),axis([0 1 0 20])
Inference:
upon examining the plot, one can see each of the transforms adheres to
the same shape, differing only in the number of samples used
to approximate that shape. In the plot one can see
the magnitude of 2 sincs with the center of the first sinc function at 0.1Fs and the
second at 0.9Fs. (sincethe data between 0:5fs and fs is a mirror image of the data
between 0 and 0:5Fs, here in this code we normalize the freq axisi.e., x-axis to 1.)

Example 2:
In the the last example the length of x[n] was limited to 3 periods in length.
Now, let's choose a large value for N (for a transform with many points), and vary
the number of repetitions of the fundamental period.
n = [0];
x1 = cos(2*pi*n/10); % 3 periods
x2 = [x1 x1]; % 6 periods
x3 = [x1 x1 x1]; % 9 periods
N = 2048; %no. of fft points
% fft magnitude depends on fft points
X1 = abs(fft(x1,N));
X2 = abs(fft(x2,N));
X3 = abs(fft(x3,N));
F = [0:N-1]/N;
subplot(3,1,1)
plot(F,X1),title('3 periods'),axis([0 1 0 50])
subplot(3,1,2)
plot(F,X2),title('6 periods'),axis([0 1 0 50])
subplot(3,1,3)
plot(F,X3),title('9 periods'),axis([0 1 0 50])

Inference:
The first plot, the transform of 3 periods of a cosine,
looks like the magnitude of 2 sincs with the center
of the first sinc at 0:1fs and the second at 0:9fs.
The second plot also has a sinc-like appearance,
but its frequency is higher and it has a larger
magnitude at 0:1fs and 0:9fs. Similarly, the third plot
has a larger sinc frequency and magnitude.
As x[n] is extended to an large number of periods,
the sincs will begin to look more and more like
impulses.

why sinc function looks more like impulse when x[n]
is extended to an large no. of periods?
Reason:
When Matlab computes the FFT, it automatically fills the
spaces from n = 30 to n = 2047 with zeros. This is like taking a
sinusoid and mulitipying it with a rectangular box of length 30.
A multiplication of a box and a sinusoid in the time domain should
result in the convolution of a sinc with impulses in the frequency domain.
Furthermore, increasing the width of the box in the time domain
should increase the frequency of the sinc in the frequency domain.

Spectrum Analysis with the FFT and Matlab
The FFT does not directly give you the spectrum of a signal.
the FFT can vary dramatically depending on the number of points (N) of the FFT,
and the number of periods of the signal that are represented.
The FFT contains information between 0 and fs, however, we know
that the sampling frequency must be at least twice the highest frequency component.
Therefore, the signal's spectrum should be entirly below fs/2 , the Nyquist frequency.
a real signal should have a transform magnitude that is symmetrical for for positive
and negative frequencies. So instead of having a spectrum that goes from 0 to fs,
it would be more appropriate to show the spectrum from -fs/2 to fs/2 .
This can be accomplished by using Matlab's fftshift function
as the following code demonstrates.

n = [0]; % 150 samples
x1 = cos(2*pi*n/10); % 15 periods i.e.,each period of 10 samples
N = 2048;
X = abs(fft(x1,N));
X = fftshift(X);
F = [-N/2:N/2-1]/N; % so that freq range is between -0.5fs to 0.5fs
plot(F,X),
xlabel('frequency / f s')

happy learning.

Added after 7 minutes:

Iam a beginner yet.
All are welcome if one like to drop some useful info regarding fft basics, spectrum analysis, zero padding, autocorrelation, PSD etc., in MATLAB.

At present i concentrate on fft basics and plots of PSD Vs freq
(freq analysis) for a while and then to zero padding.

Can any one specify popular site or pdf regarding fft

Thanks in advance.
 

fft matlab example

Any other exemple regarding Phanse noise, rms and rms noise?
 

matlab fft

sorry i made a mistake while in my previous post

n = [0:29]; % no. of samples.

In the Spectrum Analysis with the FFT and Matlab
n=[0:149]; % no. of samples
 


spectrum fft matlab

What can cause error in the calculated FFT magnitudes( i.e. root sum square of the real and complex components of the FFT)?

I have time domain data that I have integrated, Hanning windowed, and run through the FFT. The error in the magnitude increases as the frequency increases.

If I Hanning Window the data to pefrom the FFT and then do the integration, I see the inverse relationship with error in magnitude i.e. the error decreases as the frequency increases.

Can someone explain this observation?
 

spectral analysis matlab

Hi ILv2Xlr8,
just post your code so that if i can find anything wrong, i shall tell you.

Happy learning
 

fft matlab

hi ILv2Xlr8,

In most cases, the spike on the spectrum for each component frequency corrsponging to particular signal freq. There will be surrounding frequencies that also have positive values on the frequency spectrum even though they are not actually present in the original signal. This phenomenon is called spectral leakage, and it occurs because of the way the Fourier transform separates the signal into windows.

This problem arises mainly, when there is a discontinuity between the wave’s position at the end of one cycle and its position at the beginning of the next. If there is a discrepancy here, spectral leakage will result. This will cause the frequency spectrum to show spurious frequencies that are not actually present in the signal.

So to reduce spectral leakage, windowing functions are used. These functions reduce the amplitude of the waveform at the beginning and end of each window thereby decreasing the amount of spectral leakage by reducing the discrepancy between the end of the cycle and the beginning.

so the magnitude of spectrum ( without windowing ) always greater than the magnitude of spectrum with windowing.

I dont know about integrating the data and hanning windowing or viceversa.

let me check about this concept soon. if i happen to know, then i will definitely reply

Happy learning
 

hanning window fft matlab

Thanks for the reply.

I understand the periodic assumption for the FFT, hence that is why I am windowing the data with a Hanning window to get a zero ended function prior to the FFT. After the FFT, I do multiply the FFT magnitues by the coherent gain factor for the hanning window. Without integration, my magnitues are correct throughout the spectrum.

The error is introduced when I integrate either in the time domain or frequency domain. I am using the Euler integration method in the time domain or simply dividing by 2pif in the frequency domain.

Please let me know any thoughts on what could cause this magnitude error?

Any advice would be greatly appreciated.

Added after 4 hours 17 minutes:

To quantify what I am observing, when I use an Euler integrator in the time domain for sinusiodal signals with 21 test frequencies, starting at 15Hz to 490Hz in 25Hz intervals, I get an increasing exponential error in the magnitudes as follows:

error=[0.03 0.03 0.7 1.3 2.1 3.1 4.4 5.9 7.6 9.6 11.9 14.5 18.1 17.5 20.7 24.4 28.5 33.2 38.4 44.2 50.7];

plot this and see what I am observing.

Any advice on the cause of this integration error and how I can correct it would be appreciated.
 

coherent integration fft matlab

thanks
can you put more AATLAB examples!!!!
 

spectrum analysis fft matlab

hi aarr

see this tutorial
 

fourier transform magnitude matlab

Hi ILv2Xlr8,
Since the spectrum analysis using windows concept is clear to both of us.
I think the problem lies in the coding of Euler integrator in the time domain for sinusiodal signals .

1. Can u tell me how to write matlab code for an Euler integrator in the time domain for sinusiodal signals with 21 test frequencies, starting at 15Hz to 490Hz in 25Hz intervals?
(i just cant get U y are deploying Euler integrator ? with that what are U trying to do with) first clarify this doubt.

Just post any paper or article supporting this concept.

Happy learning.
 

espectrum center matlab code

Here is how you can do that.
Create an array with your frequencies of interest,
do a loop that creates a sinsoidal signal using the frequencies in the array,
integrate this signal,
window this signal,
perform FFT,
and calculate RMS FFT magnitudes including coherent gain factor of hanning window,
store these results in another array,
do this all again for the next frequency of interest in the array,
and finally compare these results with the theoritcal caluclated magnitudes to get the error.

An euler integrator is one of the simplest to code as it is simply the current sample multiplied by the sampling time + the previous integrated value.

I guess my question boils down to what causes the integration error and how can I correct it?
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top