If x[n] = sin(0.3πn) + 2 cos(0.4πn). How to Generate 500 samples of x[n] and plot them?

77 views Asked by At

I have designed a Parks-McClellan filter using the code below in MATLAB----

I want to create the above mentioned signal (x(n)) and then pass it through this filter to check the output. But I am not sure how to generate the signal and then how to pass it through the filter in matlab.

    clc; clear all; close all;

    wp =0.36;
    ws =0.4;
    Rp =0.1;
    As = 40;
    delta1 = (10^(Rp/20)-1)/(10^(Rp/20)+1);
    delta2 = (1+delta1)*10^(-As/20);

    f = [wp ws];
    m =[1 0];
    DEV =[delta1 delta2];
    [M F0 A0 W0]=firpmord(f, m, DEV);
    h = firpm(M, F0, A0, W0);
    [H, W]= freqz(h,1);
    plot(W/pi, 20*log10(abs(H)))
    grid minor
1

There are 1 answers

0
MichaelTr7 On

Signal Processing Toolbox and filter() Function

To declare the signal, x[n] a vector can be created in terms of n that ranges from 0 to 499. Using element-wise multiplication in the equation of x[n] .* will allow you to evaluate the function for every point/value of n.


Using the filter() function maybe an option if you have the Signal Processing Toolbox installed. The filter() function allows you to pass the coefficients of the filter, h and signal you wish to filter out. This function returns back the filtered signal in this exampled declared as variable y. Below is a plot that shows the original vs the filtered out signal:

Filtered Signal Plot

clc; clear; close all;

wp = 0.36;
ws = 0.4;
Rp = 0.1;
As = 40;
delta1 = (10^(Rp/20)-1)/(10^(Rp/20)+1);
delta2 = (1+delta1)*10^(-As/20);

f = [wp ws];
m =[1 0];
DEV =[delta1 delta2];
[M, F0, A0, W0]=firpmord(f, m, DEV);
h = firpm(M, F0, A0, W0);
[H, W]= freqz(h,1);
plot(W/pi, 20*log10(abs(H)));
grid minor

n = (0: 499);
x = sin(0.3*pi.*n) + 2*cos(0.4*pi.*n);
y = filter(h,1,x);

subplot(2,1,1); plot(x);
title("Original Signal");
xlabel("Sample Index [n]"); ylabel("Amplitude");

subplot(2,1,2); plot(y);
title("Filtered Signal");
xlabel("Sample Index [n]"); ylabel("Amplitude");