Why does my code for frequency analysis work only with signals obtained by the combination of sinusoids?
6 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Hi guys, I'm getting crazy with that... Here is my code:
clear all
close all
clc
load('forum.mat')
Fs = 1000; % Sampling frequency
frq_res = 0.01; %desired frequency resolution
t=0 : 1/Fs : 1/frq_res-1/Fs; %time
% X = 220*sin(2 * pi * 50 * t);
%
% x1 = 200*sin(2*pi*100*t + 30*(pi/180));
% x2 = 10*sin(2*pi*200*t + 50*(pi/180));
% x3 = 05*sin(2*pi*300*t + 90*(pi/180));
%
% % adding the harmonics
% Xt = X + x1 + x2 + x3;
Xt = i1;
Y=fft(Xt); %FFT
df=Fs/length(Y); %frequency resolution
f=(0:1:length(Y)/2-1)*df; %frequency axis
subplot(2,1,1);
M=abs(Y)/length(Xt)*2; %amplitude spectrum
M_rounded = int16(M(1:size(f, 2))); %Limit the frequency range
ind = find(M_rounded ~= 0);
stem(f(ind), M(ind), 'LineWidth', 0.5);
grid on;
xlabel('Frequency (Hz)')
ylabel('Magnitude');
subplot(2,1,2);
P=angle(Y)*180/pi; %phase spectrum (in deg.)
stem(f(ind), P(ind), 'LineWidth', 0.5);
grid on;
xlabel('Frequency (Hz)');
ylabel('Phase (degree)');
If I use Xt = X + x1 + x2 + x3 the code works good, If I use Xt = i1, with i1 obtained from another script (with the same sampling period and time definition), the code gives a wrong result. I attach .mat file of i1. The behaviour of i1 over time is the following:

Please help me, it is important for me
2 commentaires
David Goodmanson
le 29 Mar 2018
Hi Edoardo,
Could you explain a bit more about the question? The i1 array from 'forum' consists of 5000 cycles of a sinusoidal wave with a period of 20 array elements (except there are mistakes at elements 446 and 736). If fs = 1000 then the fft should should show a single peak at 50 Hz, which it does. There is nothing so far that resembles your time plot.
Réponses (2)
Edoardo Matteo Marchica
le 29 Mar 2018
Modifié(e) : Edoardo Matteo Marchica
le 29 Mar 2018
0 commentaires
David Goodmanson
le 29 Mar 2018
Hi Edoardo,
Is there is something going on in your code so that you are not looking at the fft that you think you are looking at? You are quite right that the distorted wave will have other frequency components than just the fundamental. Here is an example that shows a sin wave and a distorted sine wave, where the distortion is less than in your example. In the fft you can see a lot of subsidiary peaks on a semilog plot.
I used fftshift to put zero frequency at the center of the array. The simple plot variable p does not attempt to scale the frequency correctly, but it does equal 0 at zero frequency. If you zoom in you on either plot you will see two peaks of amplitude 1/2, one at positive frequency and one at negative frequency.
x = (0:.001:.999);
n = length(x);
y = -sin(2*pi*x);
y1 = y;
y1(abs(y)<.015) = 0;
y1(abs(y-.85)<.015)= .85;
y1(abs(y+.85)<.015)= -.85;
figure(1)
plot(x,y1) % distorted
z = fftshift(fft(y))/n;
z1 = fftshift(fft(y1)/n);
p = -n/2:n/2-1;
figure(2)
subplot(2,1,1) % undistorted
semilogy(p,abs(z))
subplot(2,1,2) % distorted
semilogy(p,abs(z1))
ylim([.8e-5,1]) % to keep irrelevant detail out of the plot
3 commentaires
David Goodmanson
le 30 Mar 2018
Hi Edoardo,
are you talking about an amplitude of 1 here compared to an amplitude of 1/2 for the code I posted?
Voir également
Catégories
En savoir plus sur Fourier Analysis and Filtering dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!