I need to write this in MATLAB. please help me write this in matlab, don\'t writ
ID: 2085605 • Letter: I
Question
I need to write this in MATLAB. please help me write this in matlab, don't write it in java or C. Thank you very much.
Write an M-file called syn.sin.m that will synthesize a waveform in the form of (7). Although for loops are rather nt 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 Function to synthesize a sum of cosine waves % usage : [xx , tt] syn-sin (fk, xk, fs, dur, tstart) = (xx, tt) syn-s in (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. 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 fs 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.Explanation / Answer
The Matlab function can be written as below. You will have to copy it in Matlab editor and then save it on your m/c. On running (f5) from the editor, you will get a pop-up to add the file to path. Select this option and you can run this function from Matalb command window for your set of vectors.
----------------------------- Matalb Function is as below -------------------------------------------------
function [xx,tt] = syn_sin(fk,Xk,fs,dur,tstart)
if (length(fk) ~= length(Xk))
ERROR('Syn_Sim', 'The Length of fk %d is not the same as the length of Xk %d', length(fk),length(Xk));
end
tt = linspace(tstart,dur+tstart,fs*dur);
xx = zeros(1,length(tt));
for i = 1:length(tt)
for j = 1:length(Xk)
xx(i) = xx(i) + Xk(j)*sin(fk(j)*tt(i));
end
end
end
--------------------------------------------------------------------------------------------
Let me know your feedback on the same.