how can i get the fft result at specific freq?

44 views (last 30 days)
nirit
nirit on 25 Dec 2016
Answered: John BG on 25 Dec 2016
how can I calculate fft on a specific frequency? I have a signal in time domain. I want to create anonymous function that calculates the fft of that signal, and returns the fft of it at w=w0.

Answers (2)

John BG
John BG on 25 Dec 2016
the FFT is the fast implementation of the DFT
function [Xk] = dft(xn,N)
% Computes Discrete Fourier Transform
% -----------------------------------
% [Xk] = dft(xn,N)
% Xk = DFT coeff. array over 0 <= k <= N-1
% xn = N-point finite-duration sequence
% N = Length of DFT
%
n = [0:1:N-1]; % row vector for n
k = [0:1:N-1]; % row vecor for k
WN = exp(-j*2*pi/N); % Wn factor
nk = n'*k; % creates a N by N matrix of nk values
WNnk = WN .^ nk; % DFT matrix
Xk = xn * WNnk; % row vector for DFT coefficients
function from recommended reading: Digital signal Processing using MATLAB, by V.Ingle J.Proakis
by constraining the key lines to a single frequency, let's say for instance N=N0 the key lines of the DFT get simplified:
WN = exp(-j*2*pi/N0); % it was a vector now just 1x1 complex
now k again is no longer a vector but a 1x1 integer
nk = n'*k;
and the matrix WNnk gets reduced to a linear vector
WNnk = WN .^ nk; % DFT on a single frequency
give me an example and I will calculate the single frequency DFT
Xk = xn * WNnk;
Bear in mind that FFT depends on the lengths of the time vector and the amount of frequency samples you are after, so outcome will vary with varying time sequence lengths, even if same t1 t2 for the input time sequence, if you sample faster or slower the result will vary.
The FFT is a significant improvement in spectrum analysis because it allows to software build many blocks that once had to be hardware, but such variations are one of the factors to correct and make sure do not render useless measurements.
So, rolling back to Start Strider answer: I agree that you'd better know the channel, band you want to work on, and then calculate as many available frequency samples as you can. Then you can take a pick, or interpolate.
if you find my answer useful would you please mark it as Accepted Answer by clicking on the ACCEPT ANSWER button?
thanks in advance for time and attention
John BG

Star Strider
Star Strider on 25 Dec 2016
I doubt you can calculate the fft (link) at a specific frequency. You have to calculate the entire Fourier transform of your signal. Then, finding the amplitude (and phase if necessary) at a specific frequency is straightforward using the interp1 function.
Example:
L = 5000; % Create Data
t = linspace(0, 10*pi, L);
y = sin(t*2) .* cos(t*5);
Ts = mean(diff(t)); % Calculate Foureir Transform
Fs = 1/Ts;
Fn = Fs/2;
Fy = fft(y)/L;
Fv = linspace(0, 1, fix(L/2)+1)*Fn;
Iv = 1:length(Fv);
aFy = abs(Fy(Iv))*2;
ampHz = @(Hz) interp1(Fv, aFy, Hz, 'linear'); % Interpolation Anonymous Function
amp60 = ampHz(60) % Find Amplitude At 60 Hz
figure(1)
semilogy(Fv, aFy, '-b')
hold on
plot(60, amp60, 'rp', 'MarkerFaceColor','g')
hold off
grid
xlabel('Frequency (Hz)')
ylabel('Amplitude')
legend('Fourier Transform', 'Amplitude at Desired Frequency')
figure(2)
plot(t, y)
grid

Products

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!