# Work with signals, frequency spectrum and frequency domain

7 views (last 30 days)
Balanica Cristian on 20 Jun 2020
Commented: Balanica Cristian on 21 Jun 2020
%% (s1):
A1=1;
F1=4;
Ucc=-2;
Fs=1000; %Ts=1/Fs;
D=2; %(seconds)
t=0:1/Fs:D;
fi=0;
%% (s2):
A2=1;
F2=4;
Fs=1000; %Ts=1/Fs;
D=2; %(seconds)
t=0:1/Fs:D;
fi=0;
%% attributes for the signal marker (s3):
L=8;
Lim=50*L;
%% the 3 signals:
s1=(A1*sin(2*pi*F1*t+fi))+Ucc;
s2=A2*sin(2*pi*F2*t+fi);
s3=s1+s2;
%% figure:
figure()
subplot(3,1,1),plot(t,s1,'r'),grid on,title('Signal s1'),xlabel('time (s)'),ylabel('Amplitude'), axis([0, D, -D-1.5, -D+1.5])
subplot(3,1,2),plot(t,s2,'m'),grid on,title('Signal s2'),xlabel('time (s)'),ylabel('Amplitude'), axis([0, D, -D+0.5, D-0.5])
subplot(3,1,3)
hold on
plot(t,s3,'b'),title('Signall s3=s1+s2'),xlabel('time (s)'),ylabel('Amplitude'), axis([0, D, -D-2.5, -D+2.5])
plot(t(Lim),s3(Lim),'y>','MarkerFaceColor','g')
hold off
yline(0)
But, instead of displaying the signals for a limited time, like 1 second, I want to display the signals as an evolution over time. And then move on to the frequency domain and provide the graphical representation of the frequency spectrum. And I don't really know how to do these things. Can it be done without Simu-link?

This version of your code should do what you expect. After the loop I wrote an example of how to go to the frequency domain
%% Initialize loop
figure()
for idxTime=1:1000
%% (s1):
A1=1;
F1=4;
Ucc=-2;
Fs=1000;
Ts=1/Fs*10; % A factor is needed, othwerwise it will be too slow
D=2; %(seconds)
t=Ts*(idxTime-1):1/Fs:D+Ts*(idxTime-1);
fi=0;
%% (s2):
A2=1;
F2=8;
Fs=1000; %Ts=1/Fs;
D=2; %(seconds)
t=Ts*(idxTime-1):1/Fs:D+Ts*(idxTime-1);
fi=0;
%% attributes for the signal marker (s3):
L=8;
Lim=50*L;
%% the 3 signals:
s1=(A1*sin(2*pi*F1*t+fi))+Ucc;
s2=A2*sin(2*pi*F2*t+fi);
s3=s1+s2;
subplot(3,1,1),plot(t,s1,'r'),grid on,title('Signal s1'),xlabel('time (s)'),ylabel('Amplitude'), axis([t(1), t(end), -D-1.5, -D+1.5])
subplot(3,1,2),plot(t,s2,'m'),grid on,title('Signal s2'),xlabel('time (s)'),ylabel('Amplitude'), axis([t(1), t(end), -D+0.5, D-0.5])
subplot(3,1,3)
hold on
plot(t,s3,'b'),title('Signall s3=s1+s2'),xlabel('time (s)'),ylabel('Amplitude'), axis([t(1), t(end), -D-2.5, -D+2.5])
plot(t(Lim),s3(Lim),'y>','MarkerFaceColor','g')
hold off
yline(0)
pause(0.001)
end
% Spectrum
FFT3 = fft(s3);
f = [0:length(s3)-1]/length(s3)*Fs;
figure,semilogx(f(1:end/2),abs(FFT3(1:end/2)))
##### 2 CommentsShowHide 1 older comment
Balanica Cristian on 21 Jun 2020
I did a little research and I managed to write the code in the end (with another values). I leave it here so that maybe the code will help someone else.
clc
clear all
close all
f1=1;
A1=10;%V
f2=20;%Mhz
A2=30;%V
f3=15;%Mhz
A3=20;%V
Fs=1000;%Mhz
t=[0:1/Fs:1];
t1=[0:1/Fs:2];
t2=[0:1/Fs:0.1];
t3=[0:1/Fs:0.2]
s1=A1*sin(2*pi*f1*t1+fi1);
s2=A2*sin(2*pi*f2*t2+fi2);
s3=A3*sawtooth(2*pi*f3*t3+fi3,0.5);
sum=A1*sin(2*pi*f1*t+fi1)+A2*sin(2*pi*f2*t+fi2)+A3*sawtooth(2*pi*f3*t+fi3,0.5);
x=10*log10(((A1^2/2+A2^2/2+A3^2/3)/1)/0.001);
figure()
subplot(4,1,1)
plot(t1,s1);
xlabel('microseconds');
ylabel('V')
subplot(4,1,2)
plot(t2,s2);
xlabel('microseconds');
ylabel('V')
subplot(4,1,3)
plot(t3,s3);
xlabel('microseconds');
ylabel('V')
subplot(4,1,4)
plot(t,sum);
xlabel('microseconds');
ylabel('V')
title(['the evolution in time of the sum of the three signals with the amplitude of 50V and',num2str(x),'dbM for a 1 ohm device impedance'])
SA = dsp.SpectrumAnalyzer('SampleRate', Fs, 'PlotAsTwoSidedSpectrum', false, 'SpectrumType', 'RMS');
tic;
while toc<10
sigData=sum.';
SA(sigData);
end

### Categories

Find more on Digital Filter Analysis in Help Center and File Exchange

R2020a

### Community Treasure Hunt

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

Start Hunting!

Translated by