expressions are equivalent if we define the complex amplitudes X as Xk = Akejk. 4.2.1 Write the Function M-file x(t) = Xke N x(t) = Ak cos(2πfkl+ok) k=1 (7) (8) Write an M-file called syn.sin.m that will synthesize a waveform in the form of (7). Although for loops are rather inefficient in MATLAB, you must write the function with one loop in this lab. The first few statements of the M-file are the comment lines-they should look like: function SYN_SIN usage: $ [xx, tt] 3 fk Xk 鲁 8 fs % dur tstart xx tt [xx, tt] = syn_sin (fk, Xk, fs, dur, tstart) Function to synthesize a sum of cosine waves syn sin (fk, Xk, fs, dur, tstart) vector of frequencies (these could be negative or positive) vector of complex amplitudes: Amp*e (j*phase) the number of samples per second for the time axis. total time duration of the signal starting time (default is zero, if you make this input optional) vector of sinusoidal values vector of times, for the time axis Note: fk and Xk must be the same length. Xk (1) corresponds to frequency fk (1), Xk (2) corresponds to frequency fk (2), etc. The MATLAB syntax length (fk) returns the number of elements in the vector fk, so we do not need a separate input argument for the number of frequencies. On the other hand, the programmer (that's you) should provide error checking to make sure that the lengths of fk and Xk are the same. See help error. Finally, notice that the input is defines the number of samples per second for the cosine generation; in other words, we are no longer constrained to using 20 samples per period. Include a copy of the MATLAB code with your lab report. 4.2.2 Default Inputs You can make the last input argument(s) take on default values if you use the nargin operator in MATLAB. For example, tstart can be made optional by including the following line of code: if nargin<5, tstart=0, end %--default value is zero 4.2.3 Testing In order to use this M-file to synthesize harmonic waveforms, you must choose the entries in the frequency vector to be integer multiples of some desired fundamental frequency. Try the following test and plot the result. [xx0, tt0] syn sin([0,100,250], [10,14*exp(-j*pi/3), 8*j1, 10000, 0.1, 0); *-Period = ? Measure the period of xx0 by hand. Then compare the period of xx0 to the periods of the three sinusoids that make up xx0, and write an explanation on the verification sheet of why the period of xx0 is longer.

C++ Programming: From Problem Analysis to Program Design
8th Edition
ISBN:9781337102087
Author:D. S. Malik
Publisher:D. S. Malik
Chapter8: Arrays And Strings
Section: Chapter Questions
Problem 24PE
icon
Related questions
Question
Please use my syn.sin.m code to answer 4.2.3. Also, please answer the explanation for the last part
function [xx, tt] = syn_sin(fk, Xk, fs, dur, tstart)
%SYN_SIN - Function to synthesize a sum of cosine waves
% usage: [xx, tt] = syn_sin(fk, Xk, fs, dur, tstart)
% fk = vector of frequencies
% Xk = vector of complex amplitudes: Amp*e^(j*phase)
% fs = number of samples per second for the time axis
% dur = total time duration of the signal
% tstart = starting time (default is zero if not provided)
% xx = vector of sinusoidal values
% tt = vector of times for the time axis
 
% Check if tstart is provided, otherwise set it to zero
if nargin < 5
tstart = 0;
end
 
% Check if fk and Xk have the same length
if length(fk) ~= length(Xk)
error('Length of frequencies (fk) and amplitudes (Xk) must be the same.');
end
 
% Generate time vector
tt = linspace(tstart, tstart + dur, dur * fs);
 
 
xx = zeros(size(tt));
 
% Loop through frequencies and add sinusoidal components
for k = 1:length(fk)
xx = xx + real(Xk(k) * exp(1j * 2 * pi * fk(k) * tt));
end
 
 
expressions are equivalent if we define the complex amplitudes X as Xk = Akejk.
4.2.1 Write the Function M-file
x(t) =
Xke
N
x(t) =
Ak cos(2πfkl+ok)
k=1
(7)
(8)
Write an M-file called syn.sin.m that will synthesize a waveform in the form of (7). Although for loops are rather
inefficient in MATLAB, you must write the function with one loop in this lab. The first few statements of the M-file are
the comment lines-they should look like:
function
SYN_SIN
usage:
$
[xx, tt]
3
fk
Xk
鲁
8
fs
%
dur
tstart
xx
tt
[xx, tt] = syn_sin (fk, Xk, fs, dur, tstart)
Function to synthesize a sum of cosine waves
syn sin (fk, Xk, fs, dur, tstart)
vector of frequencies
(these could be negative or positive)
vector of complex amplitudes: Amp*e (j*phase)
the number of samples per second for the time axis.
total time duration of the signal
starting time (default is zero, if you make this input optional)
vector of sinusoidal values
vector of times, for the time axis
Note: fk and Xk must be the same length.
Xk (1) corresponds to frequency fk (1),
Xk (2) corresponds to frequency fk (2), etc.
The MATLAB syntax length (fk) returns the number of elements in the vector fk, so we do not need a separate input
argument for the number of frequencies. On the other hand, the programmer (that's you) should provide error checking
to make sure that the lengths of fk and Xk are the same. See help error. Finally, notice that the input is defines
the number of samples per second for the cosine generation; in other words, we are no longer constrained to using 20
samples per period.
Include a copy of the MATLAB code with your lab report.
4.2.2 Default Inputs
You can make the last input argument(s) take on default values if you use the nargin operator in MATLAB. For
example, tstart can be made optional by including the following line of code:
if nargin<5, tstart=0, end %--default value is zero
4.2.3 Testing
In order to use this M-file to synthesize harmonic waveforms, you must choose the entries in the frequency vector to
be integer multiples of some desired fundamental frequency. Try the following test and plot the result.
[xx0, tt0] syn sin([0,100,250], [10,14*exp(-j*pi/3), 8*j1, 10000, 0.1, 0);
*-Period = ?
Measure the period of xx0 by hand. Then compare the period of xx0 to the periods of the three sinusoids that make
up xx0, and write an explanation on the verification sheet of why the period of xx0 is longer.
Transcribed Image Text:expressions are equivalent if we define the complex amplitudes X as Xk = Akejk. 4.2.1 Write the Function M-file x(t) = Xke N x(t) = Ak cos(2πfkl+ok) k=1 (7) (8) Write an M-file called syn.sin.m that will synthesize a waveform in the form of (7). Although for loops are rather inefficient in MATLAB, you must write the function with one loop in this lab. The first few statements of the M-file are the comment lines-they should look like: function SYN_SIN usage: $ [xx, tt] 3 fk Xk 鲁 8 fs % dur tstart xx tt [xx, tt] = syn_sin (fk, Xk, fs, dur, tstart) Function to synthesize a sum of cosine waves syn sin (fk, Xk, fs, dur, tstart) vector of frequencies (these could be negative or positive) vector of complex amplitudes: Amp*e (j*phase) the number of samples per second for the time axis. total time duration of the signal starting time (default is zero, if you make this input optional) vector of sinusoidal values vector of times, for the time axis Note: fk and Xk must be the same length. Xk (1) corresponds to frequency fk (1), Xk (2) corresponds to frequency fk (2), etc. The MATLAB syntax length (fk) returns the number of elements in the vector fk, so we do not need a separate input argument for the number of frequencies. On the other hand, the programmer (that's you) should provide error checking to make sure that the lengths of fk and Xk are the same. See help error. Finally, notice that the input is defines the number of samples per second for the cosine generation; in other words, we are no longer constrained to using 20 samples per period. Include a copy of the MATLAB code with your lab report. 4.2.2 Default Inputs You can make the last input argument(s) take on default values if you use the nargin operator in MATLAB. For example, tstart can be made optional by including the following line of code: if nargin<5, tstart=0, end %--default value is zero 4.2.3 Testing In order to use this M-file to synthesize harmonic waveforms, you must choose the entries in the frequency vector to be integer multiples of some desired fundamental frequency. Try the following test and plot the result. [xx0, tt0] syn sin([0,100,250], [10,14*exp(-j*pi/3), 8*j1, 10000, 0.1, 0); *-Period = ? Measure the period of xx0 by hand. Then compare the period of xx0 to the periods of the three sinusoids that make up xx0, and write an explanation on the verification sheet of why the period of xx0 is longer.
Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 1 steps

Blurred answer
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
C++ Programming: From Problem Analysis to Program…
C++ Programming: From Problem Analysis to Program…
Computer Science
ISBN:
9781337102087
Author:
D. S. Malik
Publisher:
Cengage Learning