%% Second-Order Section Filtering
% Load chirp.mat. The file contains a signal, y, that has most of its power above Fs/4, or half the Nyquist frequency. The sample rate is 8192 Hz.

load chirp

t = (0:length(y)-1)/Fs;

%
% Design a seventh-order Butterworth highpass filter to attenuate the components of the signal below Fs/4. Use a normalized cutoff frequency of 0.48π rad/sample. Express the filter coefficients in terms of second-order sections.

[zhi,phi,khi] = butter(7,0.48,'high');
soshi = zp2sos(zhi,phi,khi);

freqz(soshi)

%
% Filter the signal. Display the original and highpass-filtered signals. Use the same y-axis scale for both plots.

outhi = sosfilt(soshi,y);

figure
subplot(2,1,1)
plot(t,y)
title('Original Signal')
ys = ylim;

subplot(2,1,2)
plot(t,outhi)
title('Highpass-Filtered Signal')
xlabel('Time (s)')
ylim(ys)

%
% Design a lowpass filter with the same specifications. Filter the signal and compare the result to the original. Use the same y-axis scale for both plots. The result is mostly noise.

[zlo,plo,klo] = butter(7,0.48);
soslo = zp2sos(zlo,plo,klo);

outlo = sosfilt(soslo,y);

subplot(2,1,1)
plot(t,y)
title('Original Signal')
ys = ylim;

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

%
% Copyright 2012 The MathWorks, Inc.