How to plot Fft of a Wilberforce Pendulum Data

2 次查看(过去 30 天)
if true
% code
endPlease, how can I import the data I generated in Excel into matlab to perform a Fourier transform of the data. I have attached the Excel file for your help. The data is from a "design of Wilberforce pendulum". Do I need to save the file in a certain format?
Your help is really appreciated. Thank you
So I have this code, but how do I bring in my data work with the code?
Fs = 1000; % Sampling frequency
Ts = 1/Fs; % Sample time
L = 5*Fs; % Length of signal
t = (0:L-1)*Ts; % Time vector
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
x_orig=0.2*cos(2*pi*1*t)+sin(2*pi*50*t)+sin(2*pi*230*t+pi/13)+cos(2*pi*240*t)+0.5*cos(2*pi*450*t)+0.5*exp(-2*t)+1;
[m,n]=size(x_orig); x=x_orig-mean(x_orig)*ones(m,n);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Plot of data
figure(1);
subplot(4,1,1); plot(t,x_orig);
subplot(4,1,2); plot(t,x);
subplot(4,1,3); plot(t(1:200),x(1:200));
y=x+1*randn(size(t)); % Random noize with a standard deviation of 1
subplot(4,1,4); plot(t(1:200),y(1:200));
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Compute FFT of y(t)
% Compute the FFT
NFFT = 2^nextpow2(L); % Next power of 2 from length of y
Y = fft(y,NFFT)/L;
f = Fs/2*linspace(0,1,NFFT/2+1);
% Plot single-sided amplitude spectrum.
figure(2)
plot(f,2*abs(Y(1:NFFT/2+1)),'r.-')
title('Single-Sided Power Spectral Density')
xlabel('Frequency (Hz)')
ylabel('|Y(f)|')
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%Butterworth low-pass filter
Fc=235;
[b1,a1] = butter(1,Fc/(Fs/2)); % 1st order butter low-pass filter
freqz(b1,a1); hold on;
[b2,a2] = butter(2,Fc/(Fs/2)); % 2nd order butter low-pass filter
freqz(b2,a2); hold on;
[b6,a6] = butter(6,Fc/(Fs/2)); % 6th order butter low-pass filter
freqz(b6,a6);
y1 = filter(b1,a1,y); Y1 = fft(y1,NFFT)/L;
y2 = filter(b2,a2,y); Y2 = fft(y2,NFFT)/L;
y6 = filter(b6,a6,y); Y6 = fft(y6,NFFT)/L;
figure(3)
plot(f,2*abs(Y(1:NFFT/2+1)),'k:'); hold on;
plot(f,2*abs(Y1(1:NFFT/2+1)),'r.-'); hold on;
plot(f,2*abs(Y2(1:NFFT/2+1)),'g.-'); hold on;
plot(f,2*abs(Y6(1:NFFT/2+1)),'b.-'); legend('Original','1st order butter', '2nd order butter', '6th order butter');
title('Single-Sided Power Spectral Density');
xlabel('Frequency (Hz)');
ylabel('|Y(f)|');
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Elliptic low-pass filter
[be6, ae6] = ellip(6,5,40,Fc/(Fs/2));
%freqz(be6,ae6);
ye6 = filter(be6,ae6,y); Ye6 = fft(ye6,NFFT)/L;
figure(4);
plot(f,2*abs(Y(1:NFFT/2+1)),'r.-'); hold on;
plot(f,2*abs(Y6(1:NFFT/2+1)),'g.-'); hold on;
plot(f,2*abs(Ye6(1:NFFT/2+1)),'b.-'); legend('Orginal','6th order butter', '6th order elliptic');
title('Single-Sided Power Spectral Density');
xlabel('Frequency (Hz)');
ylabel('|Y(f)|');
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Butterworth stop filter
[bs3,as3] = butter(3,[0.4 0.6], 'stop'); % 6th order butter stop filter
%freqz(bs3,as3);
ys3 = filter(bs3,as3,y); Ys3 = fft(ys3,NFFT)/L;
figure(5)
plot(f,2*abs(Y(1:NFFT/2+1)),'r.-'); hold on;
plot(f,2*abs(Ys3(1:NFFT/2+1)),'g.-'); legend('Original', '6th order butter stop');
title('Single-Sided Power Spectral Density')
xlabel('Frequency (Hz)')
ylabel('|Y(f)|')
% Butterworth band-pass filter
[bp3,ap3] = butter(3,[0.4 0.6]); % 6th order butter pass filter
%freqz(bp3,ap3);
yp3 = filter(bp3,ap3,y); Yp3 = fft(yp3,NFFT)/L;
figure(6)
plot(f,2*abs(Y(1:NFFT/2+1)),'r.-'); hold on;
plot(f,2*abs(Yp3(1:NFFT/2+1)),'g.-'); legend('Original', '6th order butter band-pass');
title('Single-Sided Power Spectral Density')
xlabel('Frequency (Hz)')
ylabel('|Y(f)|')

回答(1 个)

Elizabeth Reese
Elizabeth Reese 2017-12-7
I would recommend starting with the Import Tool. This will let you easily import the XLS data as a table, numeric matrix, or column vectors. Line 407 in your data looks empty, so you can decide how MATLAB handles importing that row. For example, you can select the Range to be A2:E1004 and then create a rule to Exclude rows with blank cells to eliminate row 407. Then you can select the Output Type to be of your preference. If you select "Column vectors" then the names of your columns (which is row 1) are taken as the new variable names for each column.
From there, it looks like you have all of the FFT code already working.
  1 个评论
biniam tsegai
biniam tsegai 2020-12-1
Dear elizabeth, where is line 407, I am trying to excess or upload data from excel as well,
and kind of understand but not exactly, if can verifiy please
thank you

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Data Import and Analysis 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by