-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCode.txt
49 lines (49 loc) · 1.15 KB
/
Code.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
MATLAB CODE : -
clc;
close all;
clear all;
%
%% simulate noisy signal
f = 10;
Fs=500;
n = [1/Fs:1/Fs:1];
x = sin(2*pi*f*n);
% add noise to the signal
y = x + rand(1,length(x));
% plot the noisy signal
subplot(2,2,1);
plot(n,y);
title(‘Noisy Signal’);
xlabel(‘Time (s)’);
ylabel(‘Amplitude’);
%% Spectral analysis of the signal
L = length(y);
NFFT = 2^nextpow2(L);
y_fft = abs(fft(y,NFFT));
% creating frequency axis
freq = Fs/2*linspace(0,1,NFFT/2+1);
% Plot single-sided amplitude spectrum.
subplot(2,2,2);
plot(freq,y_fft(1:NFFT/2+1));
title(‘Single-Sided Amplitude Spectrum of y(t)’);
xlabel(‘Frequency (Hz)’);
ylabel(‘|Y(f)|’);
%% Design Filter and apply on the sequence
z = 5;
wn = [3 7]*2/Fs;
[b,a] = butter(z,wn,’low’);
% see frequency response of the filter
[h,w] = freqz(b,a,1024,Fs);
subplot(2,2,3);
plot(w,20*log10(abs(h)));
title(‘Magnitude Response of the Filter’);
xlabel(‘Frequency (Hz)’);
ylabel(‘Magnitude’);
grid on;
% Filter the signal
y_filt = filter(b,a,y);
subplot(2,2,4);
plot(n,y_filt);
title(‘Filtered Signal’);
xlabel(‘Time (s)’);
ylabel(‘Amplitude’);