Modify the following MATLAB code to get a triangle. Take a screenshot of the result. FUNCTION TESTBENCH function [xa,xd,

Business, Finance, Economics, Accounting, Operations Management, Computer Science, Electrical Engineering, Mechanical Engineering, Civil Engineering, Chemical Engineering, Algebra, Precalculus, Statistics and Probabilty, Advanced Math, Physics, Chemistry, Biology, Nursing, Psychology, Certifications, Tests, Prep, and more.
Post Reply
answerhappygod
Site Admin
Posts: 899604
Joined: Mon Aug 02, 2021 8:13 am

Modify the following MATLAB code to get a triangle. Take a screenshot of the result. FUNCTION TESTBENCH function [xa,xd,

Post by answerhappygod »

Modify the following MATLAB code to get atriangle. Take a screenshot of the result.
FUNCTION
TESTBENCH
function [xa,xd,ya,yd]=Lab7(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]=Lab7(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')
Join a community of subject matter experts. Register for FREE to view solutions, replies, and use search function. Request answer by replying!
Post Reply