www.gusucode.com > matlab 案例源码 matlab代码程序 > matlab/ApplyIdealNotchAndPassFiltersExample.m

    %% Apply Ideal Notch and Pass Filters
% This example first applies an ideal notch filter to the data in |count.dat|.
% Then, it applies a pass filter to the data.

% Copyright 2015 The MathWorks, Inc.


%%
% Load the count matrix into the workspace:

load count.dat

%%
% Create a timeseries object from column one of this matrix. 
% Specify a time vector that ranges from 1 to 24 s in 1-s intervals. 

count1 = timeseries(count(:,1),1:24);

%%
% Obtain the mean of the data:

countmean = mean(count1);

%%
% Enter the frequency interval, in hertz, for filtering the data:
interval = [0.08 0.2];

%%
% Invoke an ideal notch filter:
idealfilter_countn = idealfilter(count1,interval,'notch');

%%
% Compare the original data and the shaped data on a line plot:

plot(count1,'-.')
grid on
hold on
plot(idealfilter_countn,'-')

%%%
% Restore the mean to the filtered data and show it on the line plot, 
% adding a legend and a title:

countn_restored = idealfilter_countn + countmean;
plot(countn_restored,':')
title('Notch Filter')
legend('Original Data','Shaped Data','Mean Restored',...
       'Location','NorthWest')

%%
% Close the Figure window:
close 
%%
% Then, repeat the process using a |pass| rather than a |notch| filter:
figure

plot(count1,'-.')
grid on
hold on

idealfilter_countp = idealfilter(count1,interval,'pass');
plot(idealfilter_countp,'-')

countp_restored = idealfilter_countp + countmean;
plot(countp_restored,':')

title('Pass Filter')
legend('Original Data','Shaped Data','Mean Restored',...
       'Location','NorthWest')