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)|')
댓글 수: 0
답변 (1개)
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
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 Center 및 File Exchange에서 Data Import and Analysis에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!