FFT at specific frequency

2 views (last 30 days)
Ilan Moshe
Ilan Moshe on 27 May 2020
Answered: Raunak Gupta on 31 May 2020
Hello,
I'm trying to get a function wich calculate the transform fourrier at specific freqency by using 3 methods, when the first we're using 2 loop,s the second one loop and the last none.
The input of the function are a vector wichs is a sum of cosines, the method choose, a vector time t, delta t and fAxis wich contains the frequency:
clc;clear;close all
delta_t=0.1;
Tlim=5;
f=[1;2];
[t,x]=getSumOfCosines(delta_t,Tlim,f);
[T,tPeriod,xPeriod] = findPeriod(t,x);
fAxis=[6,8,92,12];
method='No Loops';
ft = myFourierTransform(method, t, x, delta_t, fAxis);
function [t,x]=getSumOfCosines(delta_t,Tlim,f)
t=0:delta_t:Tlim;
x=sum(cos(2*pi*t.*f));
end
function [ft] = myFourierTransform(method, t, x, delta_t, fAxis)
if strcmp(method,'2 Loops')
ft=zeros(length(fAxis),length(t));
for frequency=1:length(fAxis)
for time=1:length(t)
ft(frequency,time)=x(time)*exp(-1i*2*pi*time*fAxis(frequency))*delta_t;
end
end
end
if strcmp(method,'1 Loops')
ft=ones(length(fAxis),length(t));
time=1:length(t);
for frequency=1:length(fAxis)
ft(frequency,:)=x(time).*exp(-1i*2*pi*time*fAxis(frequency))*delta_t;
end
end
if strcmp(method,'No Loops')
ft=ones(length(fAxis),length(t));
frequency=1:length(fAxis);
time=1:length(t);
ind_frequency=1:length(fAxis);
ft(ind_frequency,:)=x(time).*exp(-1i*2*pi*time.*fAxis(frequency))*delta_t;
end
end
But I got this error about the no loop method:
Matrix dimensions must agree.
Error in myFourierTransform (line 23)
ft(ind_frequency,:)=x(time).*exp(-1i*2*pi*time.*fAxis(frequency))*delta_t;
Error in a (line 10)
ft = myFourierTransform(method, t, x, delta_t, fAxis);

Answers (1)

Raunak Gupta
Raunak Gupta on 31 May 2020
Hi Ilan,
In the last ‘No Loop’ Method, time and fAxis(frequency) are two different length vectors and you cannot get elementwise multiplication for those. On the Left hand side, ft is a matrix so I see you are trying to get a same size matrix as of ft with this expression exp(-1i*2*pi*time.*fAxis(frequency)). There is a small change you can make that will solve the error.
ft(ind_frequency,:)=x(time).*exp(-1i*2*pi*time.*(fAxis(frequency).'))*delta_t;
Here I have done transpose of fAxis(frequency) so that multiplication of two vector of size 1 x N and 1 x M will result into a matrix of N x M or M x N based on the order.

Community Treasure Hunt

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

Start Hunting!