Write a function that will generate a single sinusoid, x(t) = Acos(2p i f t + ph
ID: 2085600 • Letter: W
Question
Write a function that will generate a single sinusoid, x(t) = Acos(2p i f t + phi), by using input arguments for frequency (f) in Hz (freq), complex amplitude X = Ae^j phi(camp), duration in second (dur) and starting time (tstart). Keep in mind that while we are generating a sinusoid, as a function of time, the result is in fact a sequence of numbers, which are the values of the function sampled at certain time instances. Therefore, we need to specify how many such values we'll generate on a unit-time basis, i.e., the so-called sampling rate (fs). We'll call this function onecos () i.e., the function call should look like onecos (freq, camp, fs, dur, tstart). The function should return two output vectors: the values of the sinusoidal signal (x) and corresponding times (t) at which the sinusoid values are known. Plot the result from the following call to test your function. {xx0, tt0] = onecos (4, 2.5 * exp (-j*pi/6), 100, 0.8, -0.15): % (freq in Hz)Explanation / Answer
freq=input('Enter the frequency:'); %Taking input of frequency
camp=input('Enter the complex amplitude:'); %Taking input of complex amplitude
fs=input('Enter the sampling frequency:'); %Taking input of sampling frequency
dur=input('Enter the duration:'); %Taking input of duration
tstart=input('Enter the starting time:'); %Taking input of starting time
[xx0,tt0]=onecos(freq,camp,fs,dur,tstart); %Calling function onecos
subplot(2,1,1)
plot(tt0,xx0) %Plotting continuos graph
title('Continuous Graph')
subplot(2,1,2)
title('Scatter Graph')
scatter(tt0,xx0) %Plotting scatter graph
title('Scatter Graph')
Matlab function- onecos
function [xx0,tt0]=onecos(freq,camp,fs,dur,tstart);
i=(dur*fs)+1;
ts=1/fs; %Sampling Duration
amp=abs(camp);
phi=(angle(camp));
tt0=ones(i,1).*tstart;
for j=1:i
tt0(j)=tt0(j)+((j-1)*ts);
end
xx0=zeros(i,1);
for j=1:i
xx0(j)=amp*cos( (2*pi*freq*tt0(j)) + phi);
end
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.