Why does my code for frequency analysis work only with signals obtained by the combination of sinusoids?

1 view (last 30 days)
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 Comments
David Goodmanson
David Goodmanson on 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.
Edoardo Matteo Marchica
Edoardo Matteo Marchica on 29 Mar 2018
hi David, thanks for your answer. Maybe I'm a bit lacking in the FFT's theory. I can't understand why it gives only a fundamental with amplitude 1, while i1 is distorted. I can define i1 as I want (I can choose the sampling frequency) but the results are always the same (only fundamental with amplitude 1 or next to 1 even if I distort more than this i1)

Sign in to comment.

Answers (2)

Edoardo Matteo Marchica
Edoardo Matteo Marchica on 29 Mar 2018
Edited: Edoardo Matteo Marchica on 29 Mar 2018
hi David, thanks for your answer. Maybe I'm a bit lacking in the FFT's theory. I can't understand why it gives only a fundamental with amplitude 1, while i1 is distorted. I can define i1 as I want (I can choose the sampling frequency) but the results are always the same (only fundamental with amplitude 1 or next to 1 even if I distort more than this i1)

David Goodmanson
David Goodmanson on 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 Comments
Edoardo Matteo Marchica
Edoardo Matteo Marchica on 29 Mar 2018
I made another attempt using this code: https://it.mathworks.com/help/matlab/ref/fft.html
clear all
close all
clc
load('forum.mat')
Fs = 16000; % Sampling frequency
T = 1/Fs; % Sampling period
L = 24000; % Length of signal
t = (0:L-1)*T; % Time vector
X = i1;
subplot(2,1,1);
plot(1000*t(1:400),X(1:400))
xlabel('t (milliseconds)')
ylabel('i1(t)')
Y = fft(X);
P2 = abs(Y/L);
P1 = P2(1:L/2+1);
P1(2:end-1) = 2*P1(2:end-1);
f = Fs*(0:(L/2))/L;
subplot(2,1,2);
semilogy(f,P1)
title('Single-Sided Amplitude Spectrum of X(t)')
xlabel('f (Hz)')
ylabel('|P1(f)|')
xlim([0 1000])
ylim([.8e-5,1])
semilogy shows the presence of harmonic but I can't understand why the fundamental amplitude is 1 (in your example it correctly decreases)... It makes no sense...
I attach 2 .mat files in which i1 has different distortions.

Sign in to comment.

Categories

Find more on Fourier Analysis and Filtering 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!