📄 fdkaiser.m
字号:
rminHlog = fdutil('rmin',Hlog);
rmaxHlog = fdutil('rmax',Hlog);
i = find((rmaxHlog - rminHlog)>=Rp);
Rp_range = [rminHlog(i(1)) rmaxHlog(i(1))];
case 2
rminHlog = flipud(fdutil('rmin',Hlog(end:-1:1)));
rmaxHlog = flipud(fdutil('rmax',Hlog(end:-1:1)));
i = find((rmaxHlog - rminHlog)>=Rp);
Rp_range = [rminHlog(i(end)) rmaxHlog(i(end))];
case 3 % bandpass
% define center of passband as frequency of maximum magnitude
[Hmax,centerInd] = max(Hlog);
rminHlog1 = fdutil('rmin',Hlog(centerInd:-1:1));
rminHlog2 = fdutil('rmin',Hlog(centerInd+1:end));
i1 = find((Hmax - rminHlog1)>=Rp);
i2 = find((Hmax - rminHlog2)>=Rp);
Rp_range = [min(Hlog([centerInd-i1(1)+1 centerInd+i2(1)])) Hmax];
case 4 % bandstop
rminHlog = fdutil('rmin',Hlog);
rmaxHlog = fdutil('rmax',Hlog);
i = find((rmaxHlog - rminHlog)>=Rp);
Rp_range = [rminHlog(i(1)) rmaxHlog(i(1))];
end
else
Rp_range = [-Rp_range 0];
end
end
if isequal(RpInd,1)
passInd = find(Hlog>=Rp_range(1));
elseif isequal(RpInd,2)
passInd = find(Hlog<=Rp_range(2));
else
passInd = find(Hlog>=Rp_range(1) & Hlog<=Rp_range(2));
end
switch type
case 1 % lowpass
if isempty(passInd)
Rp_range = Hlog([1 2]);
passInd = [1 2];
end
passIndEdges = find(diff(passInd)>1);
if ~isempty(passIndEdges)
passInd = passInd(1:passIndEdges(1));
end
if isempty(stopInd)
Rs = -max(Hlog([end-1 end]));
stopInd = length(Hlog)+[-1 0];
end
stopIndEdges = find(diff(stopInd)>1);
if ~isempty(stopIndEdges)
stopInd = stopInd(stopIndEdges(end)+1:end);
end
fm = ff([passInd(end); stopInd(1)]);
Rp_range(1) = min(Hlog(1:passInd(end)));
Rp_range(2) = max(Hlog(1:passInd(end)));
Rs = -max([Hlog(stopInd(1):end); -Rs]);
case 2 % highpass
if isempty(passInd)
Rp_range = Hlog(end - [1 0]);
passInd = length(Hlog) - [1 0];
end
passIndEdges = find(diff(passInd)>1);
if ~isempty(passIndEdges)
passInd = passInd(passIndEdges(end)+1:end);
end
if isempty(stopInd)
Rs = -max(Hlog([1 2]));
stopInd = [1 2];
end
stopIndEdges = find(diff(stopInd)>1);
if ~isempty(stopIndEdges)
stopInd = stopInd(1:stopIndEdges(1));
end
fm = ff([stopInd(end); passInd(1)]);
Rp_range(1) = min(Hlog(passInd(1):end));
Rp_range(2) = max(Hlog(passInd(1):end));
Rs = -max([Hlog(1:stopInd(end)); -Rs]);
case 3 % bandpass
% index of beginning of upper stop band
% i = min(find(stopInd>=passInd(1)));
% fm = ff([stopInd(i-1); passInd([1 end]); stopInd(i)]);
[Hmax,centerInd] = max(Hlog);
if isempty(passInd)
passInd = centerInd+[0 1]';
Rp_range = sort(Hlog(passInd));
end
passIndEdges = find(diff(passInd)>1);
if ~isempty(passIndEdges)
% reduce passband to region that includes centerInd
passIndEdges = [passIndEdges(:)'; passIndEdges(:)'+1];
passIndEdges = [1; passIndEdges(:); length(passInd)];
ii = find(centerInd>passInd(passIndEdges(1:2:end)) & ...
centerInd<=passInd(passIndEdges(2:2:end)));
if isempty(ii) % current passband does not include centerInd -
% need to make sure it does
Rp_range(2) = Hmax;
if isequal(RpInd,2)
passInd = find(Hlog<=Rp_range(2));
else
passInd = find(Hlog>=Rp_range(1) & Hlog<=Rp_range(2));
end
if isempty(passInd)
passInd = centerInd+[0 1];
Rp_range = sort(Hlog(passInd));
end
passIndEdges = find(diff(passInd)>1);
if ~isempty(passIndEdges)
% reduce passband to region that includes centerInd
passIndEdges = [passIndEdges(:)'; passIndEdges(:)'+1];
passIndEdges = [1; passIndEdges(:); length(passInd)];
ii = find(centerInd>passInd(passIndEdges(1:2:end)) & ...
centerInd<=passInd(passIndEdges(2:2:end)));
passInd = passInd(passIndEdges(2*ii-1):passIndEdges(2*ii));
end
else
passInd = passInd(passIndEdges(2*ii-1):passIndEdges(2*ii));
end
end
if isempty(stopInd)
stopInd = [1 2 length(Hlog)+[-1 0]]';
Rs = -max(Hlog(stopInd));
end
i = min(find(stopInd>=centerInd));
if isempty(i) % no upper stopband!
stopInd = [stopInd; length(Hlog)+[-1 0]'];
Rs = -max(Hlog(stopInd));
i = length(stopInd)-1;
elseif i==1 % no lower stopband!
stopInd = [1; 2; stopInd];
Rs = -max(Hlog(stopInd));
i = 3;
end
stopInd1 = stopInd(1:i-1);
stopInd2 = stopInd(i:end);
stopIndEdges1 = find(diff(stopInd1)>1);
if ~isempty(stopIndEdges1)
stopInd1 = 1:stopInd1(stopIndEdges1(1));
end
stopIndEdges2 = find(diff(stopInd2)>1);
if ~isempty(stopIndEdges2)
stopInd2 = stopInd2(stopIndEdges2(end)+1):length(Hlog);
end
fm = ff([stopInd1(end); passInd([1 end]); stopInd2(1)]);
Rp_range(1) = min(Hlog(passInd));
Rp_range(2) = max(Hlog(passInd));
Rs = -max([Hlog([stopInd1(:); stopInd2(:)]); -Rs]);
case 4 % bandstop
% define center of stopband as frequency where peak of minimum magnitude
% occurs
[peakInd,peaks] = fdutil('findpeaks',Hlog);
[Hmin,ind] = min(peaks);
centerInd = peakInd(ind);
if isempty(passInd)
passInd = [1 2 length(Hlog)+[-1 0]]';
Rp_range = [min(Hlog(passInd)) max(Hlog(passInd))];
end
if isempty(stopInd)
stopInd = centerInd + [-1 0]';
Rs = -Hmin;
end
stopIndEdges = find(diff(stopInd)>1);
if ~isempty(stopIndEdges)
% reduce passband to region that includes centerInd
stopIndEdges = [stopIndEdges(:)'; stopIndEdges(:)'+1];
stopIndEdges = [1; stopIndEdges(:); length(stopInd)];
ii = find(centerInd>stopInd(stopIndEdges(1:2:end)) & ...
centerInd<=stopInd(stopIndEdges(2:2:end)));
if isempty(ii) % current stopband does not include centerInd -
% need to make sure it does
Rs = -Hmin;
stopInd = find(Hlog<=-Rs);
stopIndEdges = find(diff(stopInd)>1);
if ~isempty(stopIndEdges)
% reduce stopband to region that includes centerInd
stopIndEdges = [stopIndEdges(:)'; stopIndEdges(:)'+1];
stopIndEdges = [1; stopIndEdges(:); length(stopInd)];
ii = find(centerInd>stopInd(stopIndEdges(1:2:end)) & ...
centerInd<=stopInd(stopIndEdges(2:2:end)));
stopInd = stopInd(stopIndEdges(2*ii-1):stopIndEdges(2*ii));
end
else
stopInd = stopInd(stopIndEdges(2*ii-1):stopIndEdges(2*ii));
end
end
i = min(find(passInd>=centerInd));
if isempty(i) % no upper passband!
passInd = [passInd; length(Hlog)+[-1 0]'];
Rp_range = [min(Hlog(passInd)) max(Hlog(passInd))];
i = length(passInd)-1;
elseif i==1 % no lower passband!
stopInd = [1; 2; stopInd];
Rp_range = [min(Hlog(passInd)) max(Hlog(passInd))];
i = 3;
end
passInd1 = passInd(1:i-1);
passInd2 = passInd(i:end);
passIndEdges1 = find(diff(passInd1)>1);
if ~isempty(passIndEdges1)
passInd1 = 1:passInd1(passIndEdges1(1));
end
passIndEdges2 = find(diff(passInd2)>1);
if ~isempty(passIndEdges2)
passInd2 = passInd2(passIndEdges2(end)+1):length(Hlog);
end
fm = ff([passInd1(end); stopInd([1 end]); passInd2(1)]);
Rp_range(1) = min(Hlog(passInd));
Rp_range(2) = max(Hlog(passInd));
Rs = -max([Hlog(stopInd); -Rs]);
end
function fChangeMeas(i,type,Fs,Lresp,L1,L2,f1,f2,f3,f4,Rp,Rs)
%fChangeMeas - interactively track response when a frequency has changed
% This function is meant to be called when the user
% a) changes the f1,f2,f3, or f4 measurement
% or b) drags the f1,f2,f3, f4 value
% Xdata, Ydata, values of other frequencies and Rp and Rs are updated as
% needed.
% Inputs:
% i - which frequency has just changed
% type - band configuration (1 = lp, 2 = hp, 3 = bp, 4 = bs)
% Fs - sampling frequency
% Lresp, L1, L2 - fdline objects
% f1,f2,f3,f4,Rp,Rs - fdmeas objects
ff = Lresp.xdata;
delf = ff(2)-ff(1); % frequency spacing
Hlog = Lresp.ydata;
[maxpass,minpass,minstop] = getMagMeasurements(ff,Hlog,type,...
f1.value,f2.value,f3.value,f4.value);
switch type
case 1 % lowpass
if i == 1 % passband edge
passbandChange = 1;
else % stopband edge
passbandChange = 0;
end
Rp.value = maxpass-minpass;
Rs.value = -minstop;
case 2 % highpass
if i == 1 % stopband edge
passbandChange = 0;
else % passband edge
passbandChange = 1;
end
Rp.value = maxpass-minpass;
Rs.value = -minstop;
case 3 % bandpass
switch i
case {1,4} % stopband edge
passbandChange = 0;
stopIndex = 1 + (i==4);
[dummy,passIndex] = max(maxpass-minpass);
case {2,3} % passband edge
passbandChange = 1;
[dummy,stopIndex] = max(minstop);
passIndex = 1 + (i==3);
end
[fm,Rp_range,Rs_val] = ...
getFreqMeasurements(passbandChange,ff,Hlog,type,...
[minpass(passIndex) maxpass(passIndex)],-minstop(stopIndex));
if passbandChange
if i == 3
f2.value = fm(2);
else
f3.value = fm(3);
end
else
if i == 4
f1.value = fm(1);
else
f4.value = fm(4);
end
end
minpass = Rp_range(1);
maxpass = Rp_range(2);
minstop = -Rs_val;
if passbandChange
Rp.value = diff(Rp_range);
else
Rs.value = Rs_val;
end
case 4 % bandstop
switch i
case {1,4} % passband edge
passbandChange = 1;
[dummy,stopIndex] = max(minstop);
passIndex = 1 + (i==4);
case {2,3} % stopband edge
passbandChange = 0;
[dummy,passIndex] = max(maxpass-minpass);
stopIndex = 1 + (i==3);
end
[fm,Rp_range,Rs_val] = ...
getFreqMeasurements(passbandChange,ff,Hlog,type,...
[minpass(passIndex) maxpass(passIndex)],-minstop(stopIndex));
if ~passbandChange
if i == 3
f2.value = fm(2);
else
f3.value = fm(3);
end
else
if i == 4
f1.value = fm(1);
else
f4.value = fm(4);
end
end
minpass = Rp_range(1);
maxpass = Rp_range(2);
minstop = -Rs_val;
if passbandChange
Rp.value = diff(Rp_range);
else
Rs.value = Rs_val;
end
end
if type > 2
fchange_ind = (i==(1:4)) * [1 4; 2 3; 2 3; 1 4];
else
fchange_ind = i;
end
updateLines(passbandChange,type,fchange_ind,f1,f2,f3,f4,Rp,Rs,Fs,[minpass maxpass])
fdutil('pokeFilterMeasurements','fdkaiser',type,f1,f2,f3,f4,Rp,Rs,Fs)
function RChangeMeas(passbandChange,type,Fs,Lresp,L1,L2,f1,f2,f3,f4,Rp,Rs,...
Rp_range,RpInd)
%RChangeMeas - interactively track response when Rp or Rs has changed
% This function is meant to be called when the user
% a) changes the Rp or Rs measurement
% or b) drags the Rp or Rs value
% Xdata, Ydata, values of other frequencies and Rp and Rs are updated as
% needed.
% Inputs:
% passbandChange - boolean == 1 for Rp, 0 for Rs, or vector [0 1] for
% both Rp and Rs
% type - band configuration (1 = lp, 2 = hp, 3 = bp, 4 = bs)
% Fs - sampling frequency
% Lresp, L1, L2 - fdline objects
% f1,f2,f3,f4,Rp,Rs - fdmeas objects
% Rp_range - two element range vector of Rp, for when dragging measurement
% line
% RpInd - when dragging passband measurement line, indicates which one:
% 1 ==> lower, 2 ==> upper
ff = Lresp.xdata;
delf = ff(2)-ff(1); % frequency spacing
Hlog = Lresp.ydata;
Rp_val = get(Rp,'value');
Rs_val = get(Rs,'value');
if nargin < 13
[fm,Rp_range,Rs_val1] = ...
getFreqMeasurements(passbandChange,ff,Hlog,type,Rp_val,Rs_val);
else
[fm,Rp_range,Rs_val1] = ...
getFreqMeasurements(1,ff,Hlog,type,Rp_range,Rs_val,RpInd);
end
Rp_val1 = diff(Rp_range);
if Rp_val1 ~= Rp_val
set(Rp,'value',Rp_val1);
end
if Rs_val1 ~= Rs_val
set(Rs,'value',Rs_val1);
end
i = []; % which frequencies have changed
switch type
case 1
if any(passbandChange==1)
f1.value = fm(1);
i = 1;
end
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -