Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

How to extract the frequency and amplitude from a FFT and save the values in separated tables?

Asked by Emerson De Souza on 18 Jun 2011

Hi,

I would like to extract the values of frequency and amplitude

from a fft and save them in separated tables.

For example, for the wave y = A1*sin(2*pi*w1*t) + A2*sin(2*pi*w2*t);

I would have the tables:

1) F, with the values w1 and w2 and another table

2) A, with the values A1 and A2.

I run the script below to ONLY DISPLAY the frequencies and amplitudes

but I don't know how to extract and save.

I wonder if someone have experience on how to write some lines to perform

this action.

Thank you in advance

Emerson

SCRIPT TO DISPLAY F AND A:

clear all;
close all;
%Wave properties
w1=5;                           % Frequency1 (Hz)
A1=1;                           % Amplitude1 (dimensionless)
w2=1;                           % Frequency2 (Hz)
A2=5;                           % Amplitude2 (dimensionless)
% Time properties
Datapoints = 1000;              % Number of recorded data;
Length=10;                      % Length (seconds);
Step= Length/Datapoints;        % Fraction of seconds
t = (0:Datapoints-1)*Step;      % Time vector
% Sum of a w1-Hz sinusoid and a w2-Hz sinusoid
y = A1*sin(2*pi*w1*t) + A2*sin(2*pi*w2*t); 
% Fourier Transformation
NFFT = Datapoints; % Next power of 2 from length of y
Y = fft(y,NFFT)/Datapoints;
fs=Datapoints/Length;  
f = fs/2*linspace(0,1,NFFT/2+1);
% Plot single-sided amplitude spectrum.
plot(f,2*abs(Y(1:NFFT/2+1))) 
axis([0 6 0 6])
title('Single-Sided Amplitude Spectrum of y(t)')
xlabel('Frequency (Hz)')
ylabel('Amplitude - |Y(f)|')

0 Comments

Emerson De Souza

Products

1 Answer

Answer by Paulo Silva on 18 Jun 2011
Accepted answer

You can use the function findpeaks of the Signal Processing Toolbox™ to find the two peaks and their frequency, if you have that toolbox read the documentation about findpeaks.

Other way of finding the frequencies after you find the f value

[B,IX] = sort(2*abs(Y(1:NFFT/2+1))); %order the amplitudes
A1=B(end); %amplitude of the first peak
A2=B(end-1); %amplitude of second peak
f1=f(IX(end)); %frequency of first peak
f2=f(IX(end-1)); %frequency of second peak

Now to save them in tables please show us one example of saved data.

Maybe this way:

AmpTab=[A1 A2];
FreTab=[f1 f2];

2 Comments

Emerson De Souza on 18 Jun 2011

Thank you Paulo Silva,
I added your suggested 5 lines at the end of the code
and made a table by collecting the individual values together as
follow:

Frequencies=[f1 f2];
Amplitudes=[AA1 AA2];

The next question is:
How do we generalize your lines to collect multiple frequencies
and amplitudes without limiting to this particular case of two f and A?

Thank you in advance for your help
Emerson

Paulo Silva on 18 Jun 2011

[B,IX] = sort(2*abs(Y(1:NFFT/2+1)));
BFloor=0.1; %BFloor is the minimum amplitude value (ignore small values)
Amplitudes=B(B>=BFloor) %find all amplitudes above the BFloor
Frequencies=f(IX(1+end-numel(Amplitudes):end)) %frequency of the peaks

Paulo Silva

Contact us