(Problem 2) Modify the L7_2 code in the textbook (which must be renamed to Lab6_problem-part2) by considering the triang
-
- Site Admin
- Posts: 899603
- Joined: Mon Aug 02, 2021 8:13 am
(Problem 2) Modify the L7_2 code in the textbook (which must be renamed to Lab6_problem-part2) by considering the triang
TESTBENCH
function [xa,xd,ya,yd]=L7_2(mode)
T=0.1;
f=1/T;
dt=0.001;
dt1=0.01;
t=0:dt:1-dt;
t1=0:dt:1-dt1;
%xa=square(2*pi*f*t); Analog signal
xa=repmat([ones(1,50) -1*ones(1,50)],1,10);
%xd=square(2*pi*f*t1); Digital signal
xd=repmat([ones(1,5) -1*ones(1,5)],1,10);
ya=zeros(1,1024);
yd=zeros(1,1024);
switch mode
case 0 %Periodic signal
%FFT of analog (FS)
ya=abs(fftshift(dt*fft(xa(1:0.1*length(xa)),128)));
%FFT od discrete (DFT)
yd=abs(fftshift(dt1*fft(xd(1:0.1*length(xd)),128)));
type1='FS';
type2='DFT';
case 1
%FFT of analog (CTFT)
xa=[xa(1:0.1*length(xa)) zeros(1,0.9*length(xa))];
%FFT of digital (DTFT)
yd=[xa(1:0.1*length(xd)) zeros(1,0.9*length(xd))];
ya=abs(fftshift(dt*fft(xa,1024)));
yd=abs(fftshift(dt1*fft(xd,1024)));
type1='CTFT';
type2='DTFT';
end
yd=repmat(yd,1,9);
f1=-1/(2*dt):1/(length(ya)*dt):1/(2*dt);
f2=-1/(2*dt1):1/(length(yd)*dt):1/(2*dt1);
clear; close all; clc;
dt=0.001;
dt1=0.01;
mode=1; %0:Periodic signal, 1:Aperiodic signal
[xa,xd,ya,yd]=L7_2(mode);
f1=-1/(2*dt):1/(length(ya)*dt):1/(2*dt);
f2=-1/(2*dt1):1/(length(yd)*dt1):1/(2*dt1);
ta=linspace(0,1,length(xa));
td=linspace(0,1,length(xd));
%Verification
subplot(2,2,1)
plot(ta,xa,'.’); grid on;
title('Analog Signal')
ylabel('Amplitude')
subplot(2,2,2)
plot(td,xd,'.');grid on;
title('Discrete Signal')
ylabel('Amplitude')
subplot(2,2,3)
plot(f1(1:end-1),ya); grid on;
title('FS/CTFT')
ylabel('Amplitude')
subplot(2,2,4)
plot(f2(1:end-1),yd); grid on;
title('DFT/DTFT')
ylabel('Amplitude')
(Problem 2) Modify the L7_2 code in the textbook (which must be renamed to Lab6_problem-part2) by considering the triangular signal shown below instead of the square wave signal specified in the code. 08 06 0.4 02 of 0 42 44 46 48 0001 002 0.03 0.04 0.05 0.06 0.07 0.08 0.09 0.1 Editor-C\Users\Axa180003\Desktop\MATLAB Examples Chapter 717_2117_2.m 17.2m X+ function [xa, xd, ya, yd]=L7_2 (mode) T.1; f-1/T; dt-0.001; dt1=0.01; t=0:dt:1-dt; t1-0:dt1:1-dt1;: xa-square (2*pi*f*t); 9-xa-repmat ([ones (1,50) -1*ones (1,50)1,1,10); Analog signal 10 1 xd-square (2*pi*f*t1); 11- xd-repmat ([ones (1,5) -1*ones (1,5) 1,1,10); Digital signal 12-ya-zeros (1,1024); 13- yd-zeros (1,1024); 14- switch mode 15- 1 2- 3- 4- 5- 6- 7- 8 case 0 Perodic signal 16-ya-abs (fftshift (dt*fft (xa (1:0.1*length (xa)), 128))); FFT of analog (FS) 17- yd-abs (fftshift (dt1 fft (xd (1:0.1 length (xd)), 128))); SFFT of discrete (DFT) 18- typel='FS'; Let T=0.1 sec and dr=0.001 see for the analog version of the signal, and drl -0.01 sec for the digital 19- type2="DFT"; version of the signal. 20- case 1 Aperiodic Signal 21- xa-[xa (1:0.1 length (xa)) zeros (1,0.9 length (xa))); 22- xd=[xa (1:0.1*length (xd)) zeros (1,0.9*length (xd))]; 23-ya-abs (fftshift (dt*fft (xa, 1024))); 24- yd-abs (fftshift (dt1 fft (xd, 1024))); 25- typel='CTET'; 26-type2=¹DTFT'; 27- end 28- yd-repmat (yd, 1,9); 29-f1--1/(2*dt):1/(length (ya) *dt):1/(2*dt); 30-12--1/(2*dt1):1/(length (yd) *dt1):1/(2*dt1); FFT of analog (CTFT) EFT of digital (DTFT) Ⓒx Editor-C:\Users\Axa 180003\Desktop\MATLAB Examples\Chapter 7L7_2\L7_2_testbench.m x L7_2_testbench.mx + 1- cle; clear; close all; 2- dt-0.001; 3- dt1=0.01; 4- 5- 6- 7- 8- 9- mode-1; 10: Periodic Signal, 1:Aperiodic signal [xa, xd, ya, yd]=L7_2 (mode); fl--1/(2*dt):1/(length (ya) *dt):1/(2*dt); f2--1/(2*dt 1):1/(length (yd) *dt 1):1/(2*dt1); ta=linspace (0, 1, length (xa)); td-linspace (0, 1, length (xd)); Werification 10 11- 12- 13- 14- 15- 16- 17- 18- ylabel('Amplitude") 19- subplot (2,2,3) 20- plot (f1 (1:end-1), ya); grid on; title('FS/CTFT') 21- ylabel('Amplitude') 25- 26- subplot (2,2,1) plot (ta, xa,'.'); grid on; title('Analog Signal') ylabel('Amplitude") subplot (2,2,2) plot (td, xd,'.');grid on; title('Discrete Signal') 22- 23- subplot (2,2,4) 24- plot (f2 (1:end-1), yd); grid on; title('DET/DTFT") ylabel('Amplitude")