How to convert Time domain to frequency domain?
14 views (last 30 days)
Show older comments
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
Answers (1)
arushi
on 6 Sep 2024
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 Comment
Walter Roberson
on 6 Sep 2024
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)
See Also
Categories
Find more on Fourier Analysis and Filtering in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!