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.

How do I scale the results of my FFT to obtain a magnitude vs. frequency plot?

Status
Not open for further replies.
matlab fft scaling

After you have built all the bins, find the bin with the highest magnitude, and scale all bins by that one - now the magnitude is scaled 0-1.
 

fft scale

"After you have built all the bins,"
Didn't get that part?? Could u put it a different way.
Or may be let me rephrase what i was asking.
In matlab using fft command to requires us to enter the number of points(for eg 512) for fft as an argument and the result of fft is 512 points So when I plot it using plot(x,y) command, the x scale is also according to 512 pts. But i need my x scale to be according to the frequency range of my orignal signal.

Regds
Magnetra
 

scaling fft

Try this:
Code:
N  = 128;       % number of points
fs = 256;       % sample rate
f1 = 60;        % signal 1 frequency
f2 = 80;        % signal 2 frequency
a1 = 1.5;       % signal 1 amplitude
a2 = 2.3;       % signal 2 amplitude
t = (0 : N-1) / fs;
y = a1*sin(2*pi*f1*t) + a2*sin(2*pi*f2*t);
h = fft(y);
% Discard duplicate upper half. Scale frequency and amplitude.
freq = fs * (0 : N/2) / N;
plot(freq, 2/N * abs(h(1 : N/2+1))); xlabel('Hertz');
 
fft scaling matlab

The code works fine. Thank u. But I have one problem, with that code, the time domain plot becomes aliased. Can we get around that.
Magnetra
 

matlab fft scale

Aliasing? The sample rate (256Hz) is significantly higher than the signal frequencies (60Hz and 80Hz). We see no aliasing (incorrect frequencies) in the FFT spectrum.
 

matlab fft frequency scale

Hmm ok i got it. So if I have to use a higher frequency for carrier such as 10kHz then my sampling frequency also should be in range greater than 10k rite?

Added after 1 minutes:

When would a frequency domain aliasing take place?
 

fft amplitude scaling

The Nyquist sampling theorm says you can capture all the signal's information by sampling faster than twice the signal's bandwidth, or else aliasing will occur. That's a powerful and useful theorm, but it can produce a spectrum that is surprising and confusing to the new student. For now, you can keep things simple by sampling at greater than twice the signal's highest frequency (instead of twice its bandwidth). In that case, you would sample your 10kHz sinewave at greater than 20ksps. Try changing the numbers in MATLAB to see what happens. (Just keep in mind that 20ksps is much faster than necessary if your signal's bandwidth is, for example, only 100Hz.)
 

fft magnitude scaling

I tried out the following code in MATLAB and the resulting plots for a 10Hz signal with sampling freq of 50sps and 200sps were obtained as shown. At 50sps the plot doesn't resemble a sinusoid. Only at 200sps the splot appears a sinusoid. What do we make of that vis-a-vis Nyquist theorem.

Code:
fs = 50; %sample frequency
freq = 10; %signal frequency
h = 1/fs;
t = 0:h:1;

y = sin(2*pi*freq*t);
clf;
plot(t,y);
axis([0 1 -1.5 1.5]);
grid on;
 

fft matlab scaling

It doesn't look like a sinewave because you are drawing straight lines between the sampled points. That's not how discrete-time sampling works. You probably need to grab a good book on digital signal processing and learn the fundamentals of sampling theory. You may find good books in the ebook forums.

For a taste of what it's all about, try replacing plot(t,y); with stem(t,y,'.');
 

    magnetra

    Points: 2
    Helpful Answer Positive Rating
how to scale the fft in matlab

Here is a link from UTexas, which provides an explanation for obtaining proper frequency scale for FFT in MATLAB

**broken link removed**

Magnetra
 

fftw scale

Code:
Fs=2000;                              % sampling frequency
Fn=Fs/2;                              % Nyquist frequency
t=0:1/Fs:1;                           % time vector sampled at Fs Hz,
                                      % length of 1 second
x = sin(2*pi*t*100)+sin(2*pi*t*200)+sin(2*pi*t*300)...
+sin(2*pi*t*400)+sin(2*pi*t*500)+ sin(2*pi*t*600)...
+sin(2*pi*t*700)+sin(2*pi*t*800) +sin(2*pi*t*900) ;            %Signal
   
NFFT=2.^(ceil(log(length(x))/log(2)));% Next highest power of 2
                                      % greater than length(x).
FFTX=fft(x,NFFT);                     % Take FFT, padding with zeros.
                                      % length(FFTX)==NFFT
NumUniquePts = ceil((NFFT+1)/2);
FFTX=FFTX(1:NumUniquePts);            % FFT is symmetric, throw away
                                      % second half
MX=abs(FFTX);                         % Take magnitude of X
MX=MX*2;                              % Multiply by 2 to take into
                                      % account the fact that we
                                      % threw out second half of 
                                      %FFTX above
MX(1)=MX(1)/2;                        % Account for endpoint
                                      % uniqueness
MX(length(MX))=MX(length(MX))/2;      % We know NFFT is even
MX=MX/length(x);                      % Scale the FFT so that it is
                                      % not a function of the length
                                      % of x.
f=(0:NumUniquePts-1)*2*Fn/NFFT;
plot(f,MX);

I tried out the above code in Matlab. and the output generated is as shown in the pic.
The signal is a sum of sinusoids with different freq but same amplitude. But the corresponding FFT, the amplitude of each component is not same. Rather they seem to follow a pattern.
Why is that? Shouldn't all component in f domain have equal amplitude?

Magnetra
[/img]
 

fft frequency scale

Further, the signal with frequency exactly half the Nyquist freq has the amplitude same in both t and f domain. Why?
 

fft scale length

Your code is doing a lot of suspicious things. Instead of trying to debug it, I inserted your signal equation into my example above:
Code:
N  = 2000;      % number of points
fs = 2000;      % sample rate
t = (0 : N-1) / fs;
y = sin(2*pi*t*100) + sin(2*pi*t*200) + sin(2*pi*t*300) ...
  + sin(2*pi*t*400) + sin(2*pi*t*500) + sin(2*pi*t*600) ...
  + sin(2*pi*t*700) + sin(2*pi*t*800) + sin(2*pi*t*900);
h = fft(y);
% Discard duplicate upper half. Scale frequency and amplitude.
freq = fs * (0 : N/2) / N;
plot(freq, 2/N * abs(h(1 : N/2+1))); xlabel('Hertz');
 
Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top