In this assignment, you will write a MATLAB function and call it in another prog
ID: 1716184 • Letter: I
Question
In this assignment, you will write a MATLAB function and call it in another program. Your function should generate the sum of N sinusoids with specified parameters. In short, you will use the following model to generate the output of the function:
X(t) = Ancos(2fnt + n) + Bn
A. Creating the function
N
X(t) = Ancos(2fnt + n) + Bn
n=1
Write a function named Sum Cosine. Your function should have the following input and output parameters:
Inputs:
• N: total number of sinusoids (integer, positive)
• A: amplitudes of sinusoidal components (N × 1 matrix with real and positive elements) • f : frequency of sinusoidal components (N × 1 matrix with real and positive elements) • phi: phase of sinusoidal components (N × 1 matrix with real and positive elements)
• B: constant component of signals (N × 1 matrix with real elements)
• Start: starting time of signals (real)
• End: end time of signals (real and greater than Start)
• fs: sampling frequency (greater than twice the maximum element in f)
Outputs:
• t: time vector ranging from Start to End with a sampling rate of fs
• X: sum of sinusoids over the specified range
Your function should verify that the input variables meet the conditions indicated in paren- theses and should generate error messages if the conditions are not met. The function should also provide a brief explanation of how it works when the command help Sum Cosine is executed in the command window.
In the body of your function, after checking the input parameters, create a time vector given the starting and ending values and the sampling rate. Next, use a for loop to implement the sum of sinusoids given by the above formula.
Explanation / Answer
MATLAB code is
clear all,
close all,
clc
N=input('Enter N value : ');
A=input('Enter A value : ');
f=input('Enter f value : ');
phi=input('Enter phi value : ');
B=input('Enter B value : ');
Start=input('Enter Start value : ');
End=input('Enter End value : ');
fs=input('Enter fs value : ');
t=Start:End
X_t=0;
for n=1:N
X=A*cos(2*pi*f*t+phi)+B;
X_t=X+X_t;
end
X_t
S=sum(X_t)
There results are
Enter N value : 10
Enter A value : 100
Enter f value : 300
Enter phi value : 60
Enter B value : 10
Enter Start value : 0
Enter End value : 10
Enter fs value : 50
t =
Columns 1 through 9
0 1 2 3 4 5 6 7 8
Columns 10 through 11
9 10
X_t =
Columns 1 through 5
-852.4130 -852.4130 -852.4130 -852.4130 -852.4130
Columns 6 through 10
-852.4130 -852.4130 -852.4130 -852.4130 -852.4130
Column 11
-852.4130
S =
-9.3765e+003