Trouble with FFT and plotting velocity data for frequency

Hi
I have recently completed an experiment to measure turbulence behind a cylinder ie a Van Karman street. A hotwire was used to gain data and the hotwire (placed downstream) was moved up and down to measure velocities at different vertical positions.
I have multiple sets of data at a specific sample rate and number of samples all for each position as the hotwire is moved. The number of samples being 1024 and sample rate 660. The velocity data was stored in a CSV file which I have coded to be read into an array by MATLAB.
My problem is, I am trying to get the FFT data for these files so that I can show how the frequency peaks vary by the position of the hotwire. I have written some code which produces the FFT but it simply does not look right and I am unsure which part is actually referring to my frequency.
Obviously a FFT produces an imaginary number too and I am getting minus values for magnitude plots which shouldn't be possible? I also get the warning "Warning: Imaginary parts of complex X and/or Y arguments ignored" so was just wondering if anyone can help me out with this one?
clear,clc
a=load('101024660allvels.csv');
%Sample rate
s=1024;
%Creates time data
t=0:1/s:1-1/s;
%Does the transform
b=abs(fft(a));
%Removes erroneous max values
b(1,:) = [0];
%plots
figure;
plot(t,b);
ylabel('Magnitude');
title('Graph for Position 0 Samples 1024, Sample Rate 660','FontSize',12)

 Accepted Answer

Your fft should be calculated as:
b=abs(fft(a)/length(a));
and your frequency vector (that I call ‘fv’ here):
fv = linspace(0, 1, length(b)/2+1)*s/2;
iv = 1:length(fv);
. . . CODE . . .
figure;
plot(fv,b(iv));
but otherwise I see no problems with your code.
Attach a representative sample (or all) of '101024660allvels.csv' (use the ‘paperclip’ or ‘staple’ icon) so we can experiment and see if there is anything that could be causing problems.

4 Comments

Chris’s ‘Answer’ moved here:
Thanks for that ! I will try! I have attached the CSV file in this comment as it contains 10 rows of 1024 columns of data. It didn't seem to go through before.
I apologise for misreading your Question. I thought ‘s’ was your sampling frequency, then went back and reread it and realised it was 660 (I assume Hz).
This code corrects those errors, and adds a ribbon plot so you can see the individual fft results for each position, rather than all of them overlapped:
[d,s,r] = xlsread('101024660allvels.csv');
Fs = 660; % Sampling Frequency
Fn = Fs/2; % Nyquist Frequency
Ts = 1/Fs; % Sampling Interval
Ld = size(d,1); % Length Of Data Vectors
t = linspace(0,Ld/Fs,Ld); % Time Vector
d = bsxfun(@minus, d, mean(d)); % Subtract Means From Each Vector
fd = fft(d)/Ld; % Calculate FFT
b = abs(fd); % Absolute Magnitudes
Fv = linspace(0, 1, Ld/2+1)*Fn; % Frequency Vector
Iv = 1:length(Fv); % Index Vector
figure(1)
plot(Fv, b(Iv,:)) % Plot FFTs (2D)
grid
xlabel('Frequency (Hz)')
ylabel('Amplitude')
figure(2)
ribbon(Fv,b(Iv,:)) % Plot FFTs (3D)
grid on
axis tight
xlabel('Position Number')
ylabel('Frequency (Hz)')
zlabel('Amplitude')
May i know why is that [d,s,r] initiated at first ?
They are the outputs of the xlsread function.

Sign in to comment.

More Answers (1)

I have modified the code and I get the attached image. I think this is correct and it gives me a peak frequency! Thank you very much!

Asked:

on 19 Nov 2014

Commented:

on 7 Mar 2019

Community Treasure Hunt

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

Start Hunting!