Write an M-file called syn sin.m that will synthesize a waveform in the form of
ID: 3789946 • Letter: W
Question
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 [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
% (these could be negative or positive)
% Xk = vector of complex amplitudes: Amp*eˆ(j*phase)
% fs = the number of samples per second for the time axis
% dur = total time duration of the signal
% tstart = starting time (default is zero, if you make this input optional)
% xx = vector of sinusoidal values
% tt = 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.
Explanation / Answer
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 (could be negative or positive)
Xk = vector of complex amplitudes: Amp*e^(j*phi)
fs = the number of samples per second for the time axis
dur = total time duration of the signal
tstart = starting time (default is 0, if you make this input
optional)
xx = vector of sinusoidal values
tt = 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.
%}
tt = tstart:1/fs:dur;
xx = zeros(1, length(tt));
if length(fk) ~= length(Xk)
disp('fk and Xk are not the same length');
return;
end
for n = 1:length(fk)
xx = xx + (Xk(n)*exp(j*2*pi*(fk(n))*tt));
end
end
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.