FFT at specific frequency

4 次查看(过去 30 天)
Ilan Moshe
Ilan Moshe 2020-5-27
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);

回答(1 个)

Raunak Gupta
Raunak Gupta 2020-5-31
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.

类别

Help CenterFile Exchange 中查找有关 Frequency Transformations 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by