good morning i am a student first year in collage. i want to analyze signals for a project using fft but for some reason the plot looks weird of the hz spectrum a very high spikes occuer at 0hz and the end of the plot causing very narrow plot. here is the code and the signal file and the plot.
clear all
xls=xlsread('signals'); %importing the excel file
xlsz=size(xls); %size of the xls
xlr=xlsz(1); %number of rows
m=1000000;% scaling factor of time (Micro)
t=1/m*xls(2:xlr,1)';
%%
signum=5; %the signal number
y=xls(2:xlr,signum+1);
%%
x=fft(y);%fourier transform
f=(0:length(x)-1)*m/length(x); % detecting the range of for frequancy
plot(f,abs(x))
please help thank you

 Akzeptierte Antwort

Star Strider
Star Strider am 22 Jun. 2020

0 Stimmen

Try this:
xls = xlsread('signals.xls');
t = [0; xls(2:end,1)]*1E-6; % Time Vector (Set Initial ‘NaN’ To 0)
Fs = 1/mean(diff(t)); % Sampling Frequency
Fn = Fs/2; % Nyquist Frequency
y = xls(:,2:end); % Signal Matrix
ym = y - mean(y); % Subtract Column Means From All Columns (Eliminates D-C Offset Effect)
L = numel(t); % Signal Lengths
FTy = fft(ym)/L; % Fourier Transform (Scaled For Length)
Fv = linspace(0, 1, fix(L/2)+1)*Fn; % Frequency Vector
Iv = 1:numel(Fv); % Index Vector
figure
plot(Fv, abs(FTy(Iv,:))*2)
grid
sep = ones(numel(Fv),1) * (1:size(FTy,2)); % ‘Separation’ Matrix — Plots Each Record In Its Column Locatioln
Fm = ones(numel(size(FTy,2)),1) * Fv; % Frequency Matrix
figure
plot3(Fm, sep, abs(FTy(Iv,:))*2)
grid on
xlabel('Frequency (Hz)')
ylabel('Record (Column #)')
zlabel('Amplitude')
My code first subracts the mean from each column in order to eliminate the D-C offset at 0 Hz. It then simply calculates the fft of each column. The ‘Fv’ vector is a vector of the frequencies for a one-sided Fourier transform, and ‘Iv’ is the vector of indices corresponding to those frequencies, so that they are plotted correctly.
With 44 records, I added the second figure in order to make the Fourier transform of each record easier to see. The ‘sep’ matrix creates offsets for each record, and ‘Fm’ is the matrix of frequency vectors for each record (both required for plot3).
.

7 Kommentare

Ali Ghazzawi
Ali Ghazzawi am 22 Jun. 2020
Wow Thx i wasnt expecting this level of an answer thank you. but i forgut to tell the first row in the signals file is just naming but its ok easliy fixable.it appers i need to study your code because i understand 60% only from the code. sorry i am still frashman.
again thank you for guidance
Star Strider
Star Strider am 22 Jun. 2020
My pleasure!
I did my best to comment-document my code. If you have any questions about it, I will do my best to explain it.
Eliminating the first row is straightforward. Change the ‘y’ assignment to:
y = xls(2:end,2:end); % Signal Matrix
then use the first row as the y-axis labels, and enlarge the plot to make them more easily visible:
sepx = 125; % Separation Multiplier
sep = ones(numel(Fv),1) * (1:size(FTy,2))*sepx; % ‘Separation’ Matrix — Plots Each Record In Its Column Locatioln
Fm = ones(numel(size(FTy,2)),1) * Fv; % Frequency Matrix
figure
plot3(Fm, sep, abs(FTy(Iv,:))*2)
grid on
xlabel('Frequency (Hz)')
ylabel('Record (Column #)')
zlabel('Amplitude')
set(gca, 'YTick',(1:size(FTy,2))*sepx, 'YTickLabel',xls(1,2:end), 'YTickLabelRotation',90, 'FontSize',6)
posv = get(gcf, 'Position');
set(gcf, 'Position',posv+[0 -200 400 200])
view(-60,20)
producing:
Image Analyst
Image Analyst am 22 Jun. 2020
Well I'll give my shot at explaining it. A constant flat signal, with no varying parts is essentially a signal at a frequency of 0 (DC). So any signal can be considered as the varying parts plus a constant part, which is the mean of the entire signal. But there is a lot of power in that DC signal part. Way more than there is in any other single frequency. And the higher it is (the more your signal is lifted above 0), the more power is in the DC frequency. So when you plot it, there is a huge spike at zero frequency which totally overwhelms the small powers in all your other frequencies, so you don't really see them. By subtracting the mean signal, you're removing the DC component so now there will be no power at zero frequency and when all the other powers for the other frequencies are plotted, you'll be able to see them better. They won't all be squashed down near the x axis like it was when it needed to show the huge spike at zero frequency.
By the way, another common method to see both the huge powers and the small powers all in one plot is to plot the log of the signal, which you can do with the semilogy() function.
Ali Ghazzawi
Ali Ghazzawi am 25 Jun. 2020
oh i got it for some reson i used to work with sin functions only and expect foruier give me sin only for some reson but now i know it gives us both sin and cosin thats why when there is a dc aka 0Hz flat function it appers spike for the dc
thanks a lot for the explainaintion
Star Strider
Star Strider am 25 Jun. 2020
As always, my pleasure!
Jonnathan Singh Alvarado
Jonnathan Singh Alvarado am 25 Okt. 2021
YOU GUYS ARE GODSENDS
Star Strider
Star Strider am 25 Okt. 2021
Thank you!
A Vote is always appreciated!
.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by