How to convert Time domain to frequency domain?

14 次查看(过去 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 个评论
Karthik Murugesh
Karthik Murugesh 2022-6-17
i tried that fft function but it didnt work out
by the way iam new to matlab i usualy use VS c++

请先登录,再进行评论。

回答(1 个)

arushi
arushi 2024-9-6,7:03
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
Walter Roberson 2024-9-6,9:18
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 CenterFile Exchange 中查找有关 Fourier Analysis and Filtering 的更多信息

产品


版本

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by