Question
Sec. 2.2 function [xx,tt] = gencos(ff,dur) % usage: xx = gencos(ff,dur) % inputs: ff: desired frequency % dur: duration of the waveform in seconds %
Sec. 2.2
function [xx,tt] = gencos(ff,dur)
% usage: xx = gencos(ff,dur)
% inputs: ff: desired frequency
% dur: duration of the waveform in seconds
% outputs: xx: vector of values of the cosine signal
tt: the vector of time samples
tt = 0:1/(100*ff):dur; %-- gives 100 samples per period
xx = real(exp(j*2*pi*ff*tt));
(a) Write a function similar to gencos given in Section 2.2 (above) that will generate a cosine of a given frequency, ff, and duration dur that is sampled with a sampling frequency fs. Both ff and fs should be inputs to the function along with dur. In other words, the rst line of your function should look like: function [xx,tt] = mycos(ff,fs,dur) Make sure that you appropriately document your code. If you prefer, you may generate the cosine directly instead of using the real part of a complex exponential as in gencos. (b) Use the function that you wrote in part (a) to create a cosine of frequency 440 Hz that is one second long and save it in the vector x1. Use a sampling frequency of 4000 Hz. Use your function to generate a second cosine of frequency 480 Hz with a duration of one second, and save it in the vector x2. Finally, add the two cosines together and save it in the vector xx. (c) Use soundsc to listen to your sum of two cosines stored in xx. Make sure that you use the correct sampling frequency in your call, >> soundsc(xx,fs) What you hear should be something similar to a telephone ringing. (d) Make a plot of the rst 1000 samples of xx as follows, >> plot(xx(1:1000)) Recall that xx(1:1000) selects the rst 1000 elements of the vector xx. Any valid colon operator may be used. For example, >> plot(xx(1000:2000)); would plot the values of xx(1000) through xx(2000). (e) Since your plot in part (d) has sample numbers along the horizontal axis, and not time, create a vector tt of length 1000 that has the correct value of time associated with each sample in xx. Note: You will need to use the sampling frequency, fs = 4000, to determine what the spacing should be between the samples in xx. Once you have created the vector tt, make a plot of the rst 1000 samples of xx as follows: >> plot(tt,xx(1:1000)) Your axis should now be labeled in seconds.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started