fourier transform simple question
Show older comments
if true
% close all;clear all;clc
data1 = importdata('short_0washer.mat','data');
data2 = importdata('short_2washer.mat','data');
data3 = importdata('short_4washer.mat','data');
data4 = importdata('short_6washer.mat','data');
data5 = importdata('short_8washer.mat','data');
data6 = importdata('short_10washer.mat','data');
t1 = data1(:,1);
t2 = data2(:,1);
t3 = data3(:,1);
t4 = data4(:,1);
t5 = data5(:,1);
t6 = data6(:,1);
Amp1 = data1(:,2);
Amp2 = data2(:,2);
Amp3 = data3(:,2);
Amp4 = data4(:,2);
Amp5 = data5(:,2);
Amp6 = data6(:,2);
Fs = 1/(t1(2) - t1(1)); % Sampling frequency
L = length(data1(:,1)); % Length of signal
f = Fs*(0:(L/2))/L;
Y1 = abs(fft(Amp1-mean(Amp1)));
Y2 = abs(fft(Amp2-mean(Amp2)));
Y3 = abs(fft(Amp3-mean(Amp3)));
Y4 = abs(fft(Amp4-mean(Amp4)));
Y5 = abs(fft(Amp5-mean(Amp5)));
Y6 = abs(fft(Amp6-mean(Amp6)));
P1 = 2*abs(Y1(1:length(f)))/L;
P2 = 2*abs(Y2(1:length(f)))/L;
P3 = 2*abs(Y3(1:length(f)))/L;
P4 = 2*abs(Y4(1:length(f)))/L;
P5 = 2*abs(Y5(1:length(f)))/L;
P6 = 2*abs(Y6(1:length(f)))/L;
hold on
plot(f,P1)
axis([0 10 0 1])
plot(f,P2)
axis([0 10 0 1])
plot(f,P3)
axis([0 10 0 1])
plot(f,P4)
axis([0 10 0 1])
plot(f,P5)
axis([0 10 0 1])
plot(f,P6)
axis([0 10 0 1])
legend('0 washer','2 washers','4 washers','6 washers','8 washers','10 washers','Locations','Best')
end


The first plot is what I got from the code provided by using the fast fourier transform. Can someone debug my code to make a plot which comes with a single vertical line that indicates frequency, which is similar to the second figure.
Answers (1)
Prajit T R
on 15 Mar 2018
Hi Lin
I'm attaching a code below which maybe what you are looking for. The plot returns the figure-1 with six signals that you have provided, but with only a single vertical line to indicate the frequency as required.
data1 = importdata('short_0washer.mat','data');
data2 = importdata('short_2washer.mat','data');
data3 = importdata('short_4washer.mat','data');
data4 = importdata('short_6washer.mat','data');
data5 = importdata('short_8washer.mat','data');
data6 = importdata('short_10washer.mat','data');
t1 = data1(:,1);
t2 = data2(:,1);
t3 = data3(:,1);
t4 = data4(:,1);
t5 = data5(:,1);
t6 = data6(:,1);
Amp1 = data1(:,2);
Amp2 = data2(:,2);
Amp3 = data3(:,2);
Amp4 = data4(:,2);
Amp5 = data5(:,2);
Amp6 = data6(:,2);
Fs = 1/(t1(2) - t1(1)); % Sampling frequency
L = length(data1(:,1)); % Length of signal
f = Fs*(0:(L/2))/L;
Y1 = abs(fft(Amp1-mean(Amp1)));
Y2 = abs(fft(Amp2-mean(Amp2)));
Y3 = abs(fft(Amp3-mean(Amp3)));
Y4 = abs(fft(Amp4-mean(Amp4)));
Y5 = abs(fft(Amp5-mean(Amp5)));
Y6 = abs(fft(Amp6-mean(Amp6)));
P1 = 2*abs(Y1(1:length(f)))/L;
P2 = 2*abs(Y2(1:length(f)))/L;
P3 = 2*abs(Y3(1:length(f)))/L;
P4 = 2*abs(Y4(1:length(f)))/L;
P5 = 2*abs(Y5(1:length(f)))/L;
P6 = 2*abs(Y6(1:length(f)))/L;
hold on
ymin=0;
ymax=1;
y=[ymin,ymax];
[val, idx] = max(P1);
x=[idx/10,idx/10];
plot(x,y)
[val, idx] = max(P2);
x=[idx/10,idx/10];
plot(x,y)
[val, idx] = max(P3);
x=[idx/10,idx/10];
plot(x,y)
[val, idx] = max(P4);
x=[idx/10,idx/10];
plot(x,y)
[val, idx] = max(P5);
x=[idx/10,idx/10];
plot(x,y)
[val, idx] = max(P6);
x=[idx/10,idx/10];
plot(x,y)
Hope this answers your query. Cheers
Categories
Find more on Mathematics 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!