Calculate Wavelength from velocity FFT

3 ビュー (過去 30 日間)
Hussein Kokash
Hussein Kokash 2023 年 1 月 24 日
Hello all,
I have multiple text files, first column positon that makes a line, 2nd is corresponding velocity at each point, third is time (repeated time for all rows so i take just the first row of each file)
I am using this code:
for i = 1:40
% check if file exists
filename = sprintf('data_%d.txt', i);
if exist(filename, 'file')
% read data from file
data = dlmread(filename, '\t');
velocity = data(:,2); % velocity data is in the second column
time = data(1,3); % time data is in the 3rd column
% perform FFT on velocity
fft_velocity = fft(velocity);
% find maximum peak frequency
[max_peak, max_peak_index] = max(abs(fft_velocity));
max_freq = max_peak_index;
% calculate wavelength
wavelength = 1/max_freq;
% save wavelength and time in a matrix
results = [results; wavelength, time];
last_wavelength = wavelength; % update last non-missing point
else
% file doesn't exist, use last non-missing point
results = [results; last_wavelength, time];
disp(sprintf('File %s does not exist, using last non-missing point...', filename))
end
end
The plot is not correct becuase of the values of amplitude
I have tried also this:
% perform FFT on velocity
[fft_position, fft_velocity] = fft(velocity);
% find maximum peak frequency
[max_peak, max_peak_index] = max(abs(fft_velocity));
max_freq = fft_position(max_peak_index);
But I am getting this error:
Too many output arguments.
Error in multiplelines (line 27)
[fft_position, fft_velocity] = fft(velocity);
How do I solve it, or better is the method I am using to find the wavelength correct?
Thanks

回答 (1 件)

Sulaymon Eshkabilov
Sulaymon Eshkabilov 2023 年 1 月 24 日
the Matlab fcn fft() computes the complex valued discrete Fourier Transform values. Thus, there is an error in your code.
To obtain frequency values you'd follow these steps. Note the lowest frequency value shows the longest wavelength and the highest frequency shows the shortest wavelegnth. Here is the simple example code showing these.
fs = 2000; % Sampling frequency
t =0:1/fs:5; % Time
F1 = 5; % Lowest frequency
F2 = 17; % Mid frequency
F3 = 55; % Highest frequecy
X = 3*sin(2*pi*t*F1)+ cos(2*pi*t*F2)+ 0.5*sin(2*pi*t*F3)+randn(size(t))*.5;
nexttile
plot(t, X)
xlim([0, 1])
xlabel('Time, [s]')
ylabel('Signal, x(t)')
N = 2^nextpow2(length(X));
Y = fft(X, N); % FFT of the signal x(t)
freq = fs*(0:(N/2))/N; % Freq values
Yp = abs(Y/N); % Absolute value or magnitude of the signal spectrum
nexttile
plot(freq,Yp(1:N/2+1))
title('FFT analysis of a signal')
xlabel('Frequency, [Hz]')
ylabel('Magnitude of signal = |Y(f)|')
grid on
xlim([0, 65])
  2 件のコメント
Hussein Kokash
Hussein Kokash 2023 年 1 月 25 日
Thank you for the reply, Can that be applied to the data detailed in the question?
Sulaymon Eshkabilov
Sulaymon Eshkabilov 2023 年 1 月 25 日
Yes.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeSpectral Estimation についてさらに検索

製品


リリース

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by