Clear Filters
Clear Filters

I have an Excel file having sample data and magnitude, I want to convert the time domain to frequency domain using FFT, The file is attached

3 views (last 30 days)
I have an Excel file having sample data and magnitude, I want to convert the time domain to frequency domain using FFT, The file is attached
The code I used is below, but this one is not giving the desire results
#CODE:
clear all
clc
anum1= xlsread('DSO0001.csv','A4:A20483');
anum2=xlsread('DSO0001.csv','C4:C20483');
anum3=smooth(anum2);
subplot(2, 1, 1);
plot(anum1,anum3)
title("Smooth Data")
xft=fft(anum2,1024);
xabs=abs(xft);
subplot(2, 1, 2)
plot(xabs)
title("FFT")

Answers (1)

Star Strider
Star Strider on 20 Mar 2022
It is probably not giving the desired result because the fft is truncated to 1024. Ths signal is much longer than that, and the ‘correct’ length of the fft is 32768, by my calculations
T1 = readtable('https://www.mathworks.com/matlabcentral/answers/uploaded_files/934554/DSO0001.CSV','VariableNamingRule','preserve')
T1 = 20480×3 table
Source CH1 CH2 ______ ___ _______ 1 0.1 -0.0052 2 0.1 -0.0076 3 0.3 -0.0072 4 0.1 -0.0072 5 0.1 -0.0068 6 0.1 -0.0052 7 0.1 -0.004 8 0.1 -0.0056 9 5.1 0.004 10 5.1 0.0028 11 5.1 0.0016 12 4.9 0.0012 13 5.1 0.0008 14 5.1 0.0012 15 5.1 0.0008 16 4.9 0.0016
L = size(T1,1);
t = T1{:,1};
s = T1{:,[2,3]};
Ts = mean(diff(t));
% Tsd = std(diff(t))
Fs = 1/Ts;
Fn = Fs/2;
figure
yyaxis left
plot(t, s(:,1))
yyaxis right
plot(t, s(:,2))
NFFT = 2^nextpow2(L)
NFFT = 32768
FTs = fft(s,NFFT)/L;
Fv = linspace(0, 1, NFFT/2+1)*Fn;
Iv = 1:numel(Fv);
figure
subplot(2,1,1)
plot(Fv, abs(FTs(Iv,1))*2)
grid
xlim([0 0.5])
xlabel('Frequency')
ylabel('Amplitude')
subplot(2,1,2)
plot(Fv, abs(FTs(Iv,2))*2)
grid
xlim([0 0.5])
xlabel('Frequency')
ylabel('Amplitude')
figure
subplot(2,1,1)
plot(Fv, abs(FTs(Iv,1))*2)
grid
xlim([0 0.005])
xlabel('Frequency')
ylabel('Amplitude')
subplot(2,1,2)
plot(Fv, abs(FTs(Iv,2))*2)
grid
xlim([0 0.005])
xlabel('Frequency')
ylabel('Amplitude')
The result appears o be appropriate otherwise, since ‘CH1’ is essentially a square wave, and the fft approximates a sinc function, as expected. The ‘CH2’ signal just appears to be noise.
.
  2 Comments
Umair Dildar
Umair Dildar on 23 Mar 2022
Thanks for the quick response. I have a question
What does this function do "Ts = mean(diff(t));"
it shows error when I run on matlab, it says
Star Strider
Star Strider on 23 Mar 2022
My pleasure!
That line calculates the sampling interval ‘Ts’ of the time vector.
You are apparently reading it into a cell array. My code does not do that (and does not use xlsread), so I cannot reproduce the error. It would be best to use my code as I wrote it to avoid such problems.
One possibility:
t = cell2mat(t);
Ts = mean(diff(t);
It may be necessary to use cell2mat the entire array.
.

Sign in to comment.

Categories

Find more on Time-Frequency Analysis 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!