%% Attenuation of Low Frequencies
% Load the MAT-file chirp. The file contains a signal, y, sampled at a frequency Fs = 8192 Hz. The signal has most of its power above Fs/4 = 2048 Hz, or half the Nyquist frequency. Add random noise to the signal.

load chirp
y = y + randn(size(y))/25;
t = (0:length(y)-1)/Fs;

%
% Design a 34th-order FIR highpass filter to attenuate the components of the signal below Fs/4. Specify a normalized cutoff frequency of 0.48, which corresponds to about 1966 Hz. Visualize the frequency response of the filter.

f = [0 0.48 0.48 1];
mhi = [0 0 1 1];
bhi = fir2(34,f,mhi);

freqz(bhi,1,[],Fs)

%
% Filter the chirp signal. Plot the signal before and after filtering.

outhi = filter(bhi,1,y);

figure
subplot(2,1,1)
plot(t,y)
title('Original Signal')
ylim([-1.2 1.2])

subplot(2,1,2)
plot(t,outhi)
title('Higpass Filtered Signal')
xlabel('Time (s)')
ylim([-1.2 1.2])

%
% Change the filter from highpass to lowpass. Use the same order and cutoff. Filter the signal again. The result is mostly noise.

mlo = [1 1 0 0];
blo = fir2(34,f,mlo);
outlo = filter(blo,1,y);

subplot(2,1,1)
plot(t,y)
title('Original Signal')
ylim([-1.2 1.2])

subplot(2,1,2)
plot(t,outlo)
title('Lowpass Filtered Signal')
xlabel('Time (s)')
ylim([-1.2 1.2])

%
% Copyright 2015 The MathWorks, Inc.