Code covered by the BSD License

### Mike Donovan (view profile)

18 Sep 2004 (Updated )

Test whether a filter's magnitude response fits within a spectral mask.

function FilterMaskFigure(b, a, x1, y1)
% overlay.  Pass in the following 4 variables:
%  B:  Filter's Numerator Coefficients
%  A:  Filter's Denominator Coefficients
%  X1:  x-axis spectral mask data
%  Y1:  y-axis spectral mask data

%% Calculate Magnitude and Frequency data for the filter
[h,w] = freqz(b,a);

%% Scale Frequency and Magnitude data for the axes
xdata1 = w/pi;
ydata1 = 20*log10(abs(h));

%% Create figure
%  Auto-generated by MATLAB on 10-Sep-2004 12:30:31

figure1 = figure;

%% Create axes
axes1 = axes(...
'XGrid','on',...
'YGrid','on',...
'Parent',figure1);
xlim(axes1,[0 1]);
ylabel(axes1,'Magnitude (dB)');
box(axes1,'on');
hold(axes1,'all');

%% Create line
line1 = line(...
xdata1,ydata1,...
'Color',[0 0 1],...
'Parent',axes1);

%% Create plot
plot1 = plot(...
x1,y1,...
'Color',[0.8706 0.4902 0],...
'LineWidth',3);

%% Create legend

%% Calculate if filter fits within mask (My code)
% The variable x1 contains the transition points for the frequency mask.  So
% first calculate the frequencies where transitions occur.  This
% calculation is required because x1 doesn't necessarily start at 0 or end
% at 1, although that's the typical case.
df=cumsum(diff(x1));
% Initialize some other variables
hmag=20*log10(abs(h));
prevwindex=0;
pass = 1;
% For each segment in the mask, first calculate the frequencies in the w array
% that fit within the frequency bounds for the mask segment.  Then
% calculate the slope and offset for the mask segment and calculate the mask
% magnitude values at all the w's within the mask boundaries.  Compare the
% calculated maskvals to the hmags at the same frequency.  If any hmags are
% greater than the maskvals the test will fail.
for (i=1:length(df))
%windex=max(find(w/pi<=df(i)));
windex=find(w/pi<=df(i),1,'last');
% When the mask segment is a vertical line don't do this calculation
if (x1(i+1)~=x1(i))
slope=(y1(i+1)-y1(i))/(x1(i+1)-x1(i));
offset=y1(i)-x1(i)*slope;
prevwindex=windex;
else
% If the mask segment is a vertical transition and there is a w
% frequency value at this boundary, compare the hmag value to both
if (w(windex)/pi==x1(i+1))
result = (hmag(windex) < y1(i+1)) && (hmag(windex) < y1(i));
end
end
pass=pass*all(result);
end
pass

%% Create textbox
if (pass==1)
annotation1 = annotation(...
figure1,'textbox',...
'Position',[0.7482 0.7056 0.1161 0.08651],...
'LineStyle','none',...
'Color',[0 0.498 0],...
'FontSize',14,...
'FontWeight','bold',...
'String',{'PASS'},...
'FitHeightToText','on');
else
annotation1 = annotation(...
figure1,'textbox',...
'Position',[0.7482 0.7056 0.1161 0.08651],...
'LineStyle','none',...
'Color',[1 0 0],...
'FontSize',14,...
'FontWeight','bold',...
'String',{'FAIL'},...
'FitHeightToText','on');
end