Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Functions allow for the efficient reuse of code and help in the organization of tasks that require many MATLAB commands to perform. When writing a

image text in transcribedimage text in transcribed

Functions allow for the efficient reuse of code and help in the organization of tasks that require many MATLAB commands to perform. When writing a function, try to keep it as general as possible, and document the function thoroughly with comments. For example, the following function generates a cosine of a given frequency and duration, XX = function [xx, tt] = gencos (ff, dur) % usage: gencos (ff, dur) % % inputs: ff: desired frequency dur: duration of the waveform in seconds g % 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)); Note that this function requires two inputs, ff and dur, that define the frequency and duration of the cosine, respectively, and it produces two outputs, xx and tt, which are the samples of the cosine and the times corresponding to these samples. After saving this function in a file as gencos.m, you may then use this function to generate a two second 440 Hz sinusoid by typing the following in the MATLAB command window, >> [xx, tt) = gencos (440, 2); Note that the second output is optional, and you could also write >> xx = gencos (440,2); but in this case you would not have the values of time associated with the samples of the cosine. You may now plot this sinusoid with plot (tt,xx), or listen to it using soundsc, >> soundsc (xx, fs) where fs is the sampling frequency. Questions: (a) What value of fs should you use in the call to soundsc above? Think about this before you try it. (Don't just "guess and check.") (b) Use the soundsc command with the of fs that you determined in part (a), and see if it sounds the way it should. You can compare to a 440 Hz tone you find on the web, or you can confirm that the duration of the tone you hear is two seconds in length. Functions allow for the efficient reuse of code and help in the organization of tasks that require many MATLAB commands to perform. When writing a function, try to keep it as general as possible, and document the function thoroughly with comments. For example, the following function generates a cosine of a given frequency and duration, XX = function [xx, tt] = gencos (ff, dur) % usage: gencos (ff, dur) % % inputs: ff: desired frequency dur: duration of the waveform in seconds g % 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)); Note that this function requires two inputs, ff and dur, that define the frequency and duration of the cosine, respectively, and it produces two outputs, xx and tt, which are the samples of the cosine and the times corresponding to these samples. After saving this function in a file as gencos.m, you may then use this function to generate a two second 440 Hz sinusoid by typing the following in the MATLAB command window, >> [xx, tt) = gencos (440, 2); Note that the second output is optional, and you could also write >> xx = gencos (440,2); but in this case you would not have the values of time associated with the samples of the cosine. You may now plot this sinusoid with plot (tt,xx), or listen to it using soundsc, >> soundsc (xx, fs) where fs is the sampling frequency. Questions: (a) What value of fs should you use in the call to soundsc above? Think about this before you try it. (Don't just "guess and check.") (b) Use the soundsc command with the of fs that you determined in part (a), and see if it sounds the way it should. You can compare to a 440 Hz tone you find on the web, or you can confirm that the duration of the tone you hear is two seconds in length

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Systems Analysis And Synthesis Bridging Computer Science And Information Technology

Authors: Barry Dwyer

1st Edition

0128054492, 9780128054499

More Books

Students also viewed these Databases questions

Question

Why are groups and teams becoming increasingly popular?

Answered: 1 week ago

Question

LO4 Specify how to design a training program for adult learners.

Answered: 1 week ago