# v_filterbank

## PURPOSE

V_FILTERBANK appply v_filterbank to a signal: [y,zo]=(b,a,x,gd)

## SYNOPSIS

function [y,zo]=v_filterbank(b,a,x,gd)

## DESCRIPTION

    V_FILTERBANK appply v_filterbank to a signal: [y,zo]=(b,a,x,gd)

     Inputs:
        b    numerator coefficients, one row per filter
        a    denominator coefficients, one row per filter
        x    input signal
        gd   group delay of each filter in samples [default=0]. The filter
             outputs will be advanced to compensate for the group delays.
             Alternatively, this input can be the zo output from a previous call.

     Outputa:
        y    output signals, one column per filter
        zo   output filter state

## CROSS-REFERENCE INFORMATION

This function calls:

- v_cblabel V_CBLABEL add a label to a colorbar C=(L,H)

This function is called by:

## SOURCE CODE

    0001 function [y,zo]=v_filterbank(b,a,x,gd)
    0002 %V_FILTERBANK appply v_filterbank to a signal: [y,zo]=(b,a,x,gd)
    0003 %
    0004 % Inputs:
    0005 %    b    numerator coefficients, one row per filter
    0006 %    a    denominator coefficients, one row per filter
    0007 %    x    input signal
    0008 %    gd   group delay of each filter in samples [default=0]. The filter
    0009 %         outputs will be advanced to compensate for the group delays.
    0010 %         Alternatively, this input can be the zo output from a previous call.
    0011 %
    0012 % Outputa:
    0013 %    y    output signals, one column per filter
    0014 %    zo   output filter state
    0015
    0016 %      Copyright (C) Mike Brookes 2009-2010
    0017 %      Version: $Id: v_filterbank.m 10865 2018-09-21 17:22:45Z dmb $
    0018 %
    0019 %   VOICEBOX is a MATLAB toolbox for speech processing.
    0020 %   Home page: http://www.ee.ic.ac.uk/hp/staff/dmb/voicebox/voicebox.html
    0021 %
    0022 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    0023 %   This program is free software; you can redistribute it and/or modify
    0024 %   it under the terms of the GNU Lesser General Public License as published by
    0025 %   the Free Software Foundation; either version 3 of the License, or
    0026 %   (at your option) any later version.
    0027 %
    0028 %   This program is distributed in the hope that it will be useful,
    0029 %   but WITHOUT ANY WARRANTY; without even the implied warranty of
    0030 %   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    0031 %   GNU Lesser General Public License for more details.
    0032 %
    0033 %   You can obtain a copy of the GNU Lesser General Public License from
    0034 %   https://www.gnu.org/licenses/ .
    0035 %
    0036 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    0037 nf=size(b,1);           % number of filters
    0038 nz=max(size(b,2),size(a,2))-1;  % size of state  needed
    0039 zzo=zeros(nz,nf);
    0040 nx=length(x);           % number of input samples
    0041 xx=x(:);
    0042 if nargin<4 || ~numel(gd)
    0043     gd=zeros(nf,1);
    0044 end
    0045 if isstruct(gd)
    0046     zi=gd.zzo;          % get filter state
    0047     qd=gd.qd;
    0048     sd=qd(2)-qd(1);     % number of output samples we need to save
    0049     qd(3)=qd(3)+nx;
    0050     rd=gd.rd;
    0051     yy=zeros(nx+sd,nf);
    0052     yy(1:sd,:)=gd.sy;
    0053     for i=1:nf
    0054         [yy(sd+1:end,i),zzo(:,i)]=filter(b(i,:),a(i,:),xx,zi(:,i));
    0055     end
    0056 else
    0057     qd=zeros(1,3);      % [min-delay max-delay #samples]
    0058     rd=round(gd);
    0059     qd(1)=min(rd);
    0060     qd(2)=max(rd);      % find the largest delay
    0061     qd(3)=nx;           % number of filtered samples
    0062     sd=qd(2)-qd(1);     % number of output samples we need to save
    0063     yy=zeros(nx+sd,nf);
    0064     for i=1:nf
    0065         [yy(sd+1:end,i),zzo(:,i)]=filter(b(i,:),a(i,:),xx);
    0066     end
    0067 end
    0068 ny=max(0,min(nx,qd(3)-qd(2)));    % numer of output samples
    0069 y=zeros(ny,nf);
    0070 if ny>0
    0071     for i=1:nf
    0072         off=rd(i)-qd(1);
    0073         y(:,i)=yy(off+1:off+ny,i);
    0074     end
    0075 end
    0076 if nargout>1
    0077     zo.zzo=zzo;  % filter state
    0078     zo.qd=qd;    % offsets
    0079     zo.rd=rd; % rounded group delays
    0080     zo.sy=yy(end-sd+1:end,:);  % save old outputs
    0081 end
    0082 if ~nargout   % plot pseudo spectrogram
    0083     ng=300;         % target number of columns in image
    0084     kd=max(1,floor(ny/ng));     % decimation factor
    0085     jm=floor(ny/kd);  % % number f frames
    0086 yd=reshape(sum(reshape(y(1:kd*jm,:).^2,kd,nf*jm),1),jm,nf)/kd;
    0087     ydm=max(yd(:));
    0088     imagesc((1:jm)*kd+qd(3)-ny-(kd-1)/2,1:nf,10*log10(max(yd,ydm/1e4))');
    0089     axis('xy');
    0090     colorbar;
    0091     v_cblabel('dB');
    0092     xlabel('Sample Number');
    0093     ylabel('Filter Channel');
    0094     title('Filterbank output');
    0095 end