middlepad.m
来自「Matlab时频分析工具箱,希望能对大家有所帮助啊」· M 代码 · 共 178 行
M
178 行
function f=middlepad(f,L,dim,centering)%MIDDLEPAD Symmetrically zero-extends or cuts a function.% Usage: h=middlepad(f,L);% h=middlepad(f,L,dim);% h=middlepad(f,L,dim,cent);%% MIDDLEPAD(f,L) cuts or zero-extends f to length L by inserting% zeros in the middle of the vector, or by cutting in the middle% of the vector.%% If f is whole-point even, MIDDLEPAD(f,L) will also be whole-point even.%% MIDDLEPAD(f,L,dim) does the same along dimension dim.% % If f has even length, then f will not be purely zero-extended, but% the last element will be repeated once and multiplied by 1/2!% That is, the support of f will increase by one!%% MIDDLEPAD(f,L,dim,cent) will cut or extend whole point even functions% if cent=0 (this is the default) or half point even functions if% cent=0.5%% SEE ALSO: ISEVEN, FIR2IIR, FFTRESAMPLE% This program is free software: you can redistribute it and/or modify% it under the terms of the GNU General Public License as published by% the Free Software Foundation, either version 3 of the License, or% (at your option) any later version.% % This program is distributed in the hope that it will be useful,% but WITHOUT ANY WARRANTY; without even the implied warranty of% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the% GNU General Public License for more details.% % You should have received a copy of the GNU General Public License% along with this program. If not, see <http://www.gnu.org/licenses/>.error(nargchk(2,4,nargin));if nargin==2 dim=[];end;if nargin<=3 centering=0;end;if (numel(L)~=1 || ~isnumeric(L)) error('L must be a scalar');end;if rem(L,1)~=0 error('L must be an integer.');end;if L<1 error('L must be larger than 0.');end;[f,L,Ls,W,dim,permutedsize,order]=assert_sigreshape_pre(f,L,dim,'MIDDLEPAD');Lorig=Ls;% Skip the main section if there is nothing to do. This is necessary% because some of the code below cannot handle the case of 'nothing to do'if L~=Ls switch centering case 0 % --------------- WPE case -------------------------------------- if Lorig==1 % Rather trivial case f=[f(1,:);zeros(L-1,W)]; else if Lorig>L % Cut if mod(L,2)==0 % L even. Use average of endpoints. f=[f(1:L/2,:);(f(L/2+1,:)+f(Lorig-L/2+1,:))/2;f(Lorig-L/2+2:Lorig,:)]; else % No problem, just cut. f=[f(1:(L+1)/2,:);f(Lorig-(L-1)/2+1:Lorig,:)]; end; else d=L-Lorig; % Extend if mod(Lorig,2)==0 % Lorig even. We must split a value. f=[f(1:Lorig/2,:);... f(Lorig/2+1,:)/2;... zeros(d-1,W);... f(Lorig/2+1,:)/2;... f(Lorig/2+2:Lorig,:)]; else % Lorig is odd, we can just insert zeros. f=[f(1:(Lorig+1)/2,:);zeros(d,W);f((Lorig+3)/2:Lorig,:)]; end; end; end; case .5 % ------------------ HPE case ------------------------------------ if Lorig==1 else if Lorig>L d=Lorig-L; % Cut if mod(L,2)==0 % L even % No problem, just cut. f=[f(1:L/2,:);... f(Lorig-L/2+1:Lorig,:);]; else % Average of endpoints. f=[f(1:(L-1)/2,:);(f((L+1)/2,:)+f(Lorig-(L-1)/2,:))/2;... f(Lorig-(L-1)/2+1:Lorig,:);]; end; else d=L-Lorig; % Extend if mod(Lorig,2)==0 % Lorig even. We can just insert zeros in the middle. f=[f(1:Lorig/2,:);... zeros(d,W);... f(Lorig/2+1:Lorig,:)]; else % Lorig odd. We need to split a value in two f=[f(1:(Lorig-1)/2,:);... f((Lorig+1)/2,:)/2;... zeros(d-1,W);... f((Lorig+1)/2,:)/2;... f((Lorig-1)/2+2:Lorig,:)]; end; end; end; otherwise error('Unsupported centering.'); end;end;f=assert_sigreshape_post(f,dim,permutedsize,order);
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?