Help with some fft problems, can't come up with a better title

2 views (last 30 days)
Hello! We have an assignment that involves some fft coding and we have some issues. The teacher gave us a code that should work, but since it's corona times and it's hard to reach him, we mostly have to solve problems using google nowadays.
The code we have, where y and fs are given vectors
figure(1)
plot(y);
Y = abs(fft(y));
plot(Y(1:length(Y)/2));
N = length(Y);
frekvens = fs*(0:(N/2))/N;
figure(2)
y = 2*abs(fft(Y))/N;
plot(frekvens,Y(N))
But we get this error message: Warning: Integer operands are required for colon operator when used as index. In this line of code: plot(Y(1:length(Y)/2));
I have googled it and it says that we should make it into an integer or something when using colons and parenthesis (floor,ceil,fix,round) but I don't know how to use it properly, or it might be another problem.
The second task seems a bit harder imo. And if you feel that you have the time to read the question and help me and my mates, that would be hugely appreciated!
In the file arn_passengers.mat is the number of passengers arriving and departing at Arlanda airport north of Stockholm. The data is assembled as date (year and month) and passengers per month. Investigate the data by plotting it. The plot show that there is a bias which is quite large as well as close to linear increase in number of passengers per month. Use fft to determine the period in months for two cases: with bias and linear trend as well as without linear trend and bias. To remove the linear trend and bias you may use the Matlab function detrend
I wrote this code, but I don't know if it's even remotly close to being correct:
D = Date;
Pass = Passengers;
Det_Pass = detrend(Passengers);
NFFT=10000; %NFFT-point DFT
X2=fft(Pass,NFFT); %compute DFT using FFT
nVals=0:NFFT-1; %DFT Sample points
plot(nVals,abs(X2));
Thanks in advance and best regards,
/ Axel

Answers (1)

Star Strider
Star Strider on 21 May 2020
You need to force the index arguments to be integers.
One option:
plot(Y(1:fix(length(Y)/2)));
There are others, depending on what you want to do.
The rest of the code appears to be correct in the sense that it should run without error. (I did not test it.) You need to determine if it produces the result you want.

Community Treasure Hunt

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

Start Hunting!