Matlab code for Addition of the signals frequnecy and amplitude value will be enter by user

Code:


clc;
clear all;
close all;
n=input('enter the no. of freq components :');
a=input('enter the amplitude matrix :');
f=input('enter the freq matrix :');
fs=input('enter the sampling frequency :');
disp(a); % display the amplitude componenets
disp(f); % display the frequency componenets
for i=1:1:n
    if (i+1)>n
        break;
    end
       if f(1,i)>f(1,i+1)
           f(1,i+1)=f(1,i); %find outthe maxium freq components
       end  
end
if fs<(2*f(1,n)) %check out the sampling theroem (fs>=2fm)
    fs=input('The fs entered was unadequate. Plz enter the required sampling frequency :');
end
z=0:(.1*fs):fs;
for i=1:1:n  %display the each cosine signal
    x=a(1,i)*cos(2*pi*(z/fs)*f(1,i));
    subplot(n+1,1,i);
    stem(z,x);
    grid on;
    hold on;
end
x=0;
for i=1:1:n  %addition of all signals
    x=(a(1,i)*cos(2*pi*(z/fs)*f(1,i)))+x;
    subplot(n+1,1,n+1);
    stem(z,x);
    grid on;
end


Output:


enter the no. of freq components :4
enter the amplitude matrix :[1 2 1 2]
enter the freq matrix :[5 6 7 5]
enter the sampling frequency :12
     1     2     1     2     5     6     7     5
The fs entered was unadequate. Plz enter the required sampling frequency :16



0 comments:

Post a Comment