How to convert Time domain to frequency domain?
15 ビュー (過去 30 日間)
古いコメントを表示
i have attached my torque data obtained from robot simulation program for all the joints.
Data need to be analyised in frequency domain, but presently it is in the time domain. It would be great if someone send me the code that actual works to convert the attached file.
I have also tried many online mat lab code which didnt work as i required.
other spec:
Sampling time - 20hz=0.05
data sets - 300
回答 (1 件)
arushi
2024 年 9 月 6 日
Based on my understanding, you have time domain data in an Excel sheet and you would like to convert it to the frequency domain. However, the Excel file contains NaN values, which can cause issues when using the “fft” function. To resolve this, it is necessary to remove the NaN values from the data before applying the fft function.
Here is the code to remove NANs and convert data to frequency domain using fft function.
file = “CF_0.1 - Copy.xls”;
% Read the data from the Excel file
data = readmatrix(file); % For MATLAB R2019b or later
matrix = data;
matrixWithoutNaNs = matrix;
matrixWithoutNaNs(isnan(matrixWithoutNaNs)) = 0;
% Display the matrix without NaNs
disp(matrixWithoutNaNs);
% using fft function
torque_fft = fft(matrixWithoutNaNs);
N = length(matrixWithoutNaNs);
fs = 20; % Sampling rate in Hz
f = (-N/2:N/2-1)*(fs/N);
% Plotting
magnitude_spectrum = abs(fftshift(torque_fft));
plot(f, magnitude_spectrum);
xlabel('Frequency (Hz)');
ylabel('Magnitude');
title('Magnitude Spectrum');
Please find the links of documentation to “isnan”, “fft” and “fftshift” functions.
1 件のコメント
Walter Roberson
2024 年 9 月 6 日
zeroing the NaN is probably the wrong thing to do. You want to skip the nan, not treat it as zero.
file = “CF_0.1 - Copy.xls”;
% Read the data from the Excel file
data = readmatrix(file); % For MATLAB R2019b or later
Fs = 20;
n = length(data);
t = (0:n-1)/Fs;
mask = isfinite(data);
matrixWithoutNaNs = data(mask);
t_withoutNaNs = t(mask);
f = (0:n-1)/n*Fs;
F = nufft(matrixWithoutNaNs, t_withoutNaNs, f);
magnitude_spectrum = abs(F);
plot(f, magnitude_spectrum)
参考
カテゴリ
Help Center および File Exchange で Fourier Analysis and Filtering についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!