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
Thank you in advance
SCRIPT TO DISPLAY F AND A:
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;
f = fs/2*linspace(0,1,NFFT/2+1);
% Plot single-sided amplitude spectrum.
axis([0 6 0 6])
title('Single-Sided Amplitude Spectrum of y(t)')
ylabel('Amplitude - |Y(f)|')
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];