[a55674]: filterbank / plotfilterbank.m Maximize Restore History

Download this file

plotfilterbank.m    226 lines (189 with data), 6.1 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
function coef = plotfilterbank(coef,a,varargin)
%PLOTFILTERBANK Plot filterbank and ufilterbank coefficients
% Usage: plotfilterbank(coef,a);
% plotfilterbank(coef,a,fc);
% plotfilterbank(coef,a,fc,fs);
% plotfilterbank(coef,a,fc,fs,dynrange);
%
% `plotfilterbank(coef,a)` plots filterbank coefficients *coef* obtained from
% either the |filterbank| or |ufilterbank| functions. The coefficients must
% have been produced with a time-shift of *a*. For more details on the
% format of the variables *coef* and *a*, see the help of the |filterbank|
% or |ufilterbank| functions.
%
% `plotfilterbank(coef,a,fc)` makes it possible to specify the center
% frequency for each channel in the vector *fc*.
%
% `plotfilterbank(coef,a,fc,fs)` does the same assuming a sampling rate of
% *fs* Hz of the original signal.
%
% `plotfilterbank(coef,a,fc,fs,dynrange)` makes it possible to specify
% the dynamic range of the coefficients.
%
% `C=plotfilterbank(...)` returns the processed image data used in the
% plotting. Inputting this data directly to `imagesc` or similar
% functions will create the plot. This is usefull for custom
% post-processing of the image data.
%
% `plotfilterbank` supports all the optional parameters of |tfplot|. Please
% see the help of |tfplot| for an exhaustive list.
%
% In addition to the flags and key/values in |tfplot|, `plotfilterbank`
% supports the following optional arguments:
%
% 'fc',fc Centre frequencies of the channels. *fc* must be a vector with
% the length equal to the number of channels. The
% default value of [] means to plot the channel
% no. instead of its frequency.
%
% 'ntickpos',n Number of tick positions along the y-axis. The
% position of the ticks are determined automatically.
% Default value is 10.
%
% 'tick',t Array of tick positions on the y-axis. Use this
% option to specify the tick position manually.
%
% 'audtick' Use ticks suitable for visualizing an auditory
% filterbank. Same as `'tick',[0,100,250,500,1000,...]`.
%
% See also: filterbank, ufilterbank, tfplot, sgram
if nargin<2
error('%s: Too few input parameters.',upper(mfilename));
end;
definput.import={'plotfilterbank','tfplot','ltfattranslate'};
definput.keyvals.xres=800;
[flags,kv]=ltfatarghelper({'fc','fs','dynrange'},definput,varargin);
if iscell(coef)
M=numel(coef);
a = comp_filterbank_a(a,M);
if any(a(:,2)~=1)
% Fractional case
L = a(1);
else
% Non-fractional case
L=a(1)*size(coef{1},1);
end
N=kv.xres;
coef2=zeros(M,N);
for ii=1:M
row=coef{ii};
if numel(row)==1
coef2(ii,:) = row;
continue;
end
coef2(ii,:)=interp1(linspace(0,1,numel(row)),row,...
linspace(0,1,N),'nearest');
end;
coef=coef2;
delta_t=L/N;
else
a=a(1);
Nc=size(coef,1);
N=kv.xres;
M=size(coef,2);
coef=interp1(linspace(0,1,Nc),coef,...
linspace(0,1,N),'nearest');
coef=coef.';
delta_t=a*Nc/N;
end;
% Freq. pos is just number of the channel.
yr=1:M;
if size(coef,3)>1
error('Input is multidimensional.');
end;
% Apply transformation to coefficients.
if flags.do_db
coef=20*log10(abs(coef)+realmin);
end;
if flags.do_dbsq
coef=10*log10(abs(coef)+realmin);
end;
if flags.do_linsq
coef=abs(coef).^2;
end;
if flags.do_linabs
coef=abs(coef);
end;
if flags.do_lin
if ~isreal(coef)
error(['Complex valued input cannot be plotted using the "lin" flag.',...
'Please use the "linsq" or "linabs" flag.']);
end;
end;
% 'dynrange' parameter is handled by converting it into clim
% clim overrides dynrange, so do nothing if clim is already specified
if ~isempty(kv.dynrange) && isempty(kv.clim)
maxclim=max(coef(:));
kv.clim=[maxclim-kv.dynrange,maxclim];
end;
% Handle clim by thresholding and cutting
if ~isempty(kv.clim)
coef(coef<kv.clim(1))=kv.clim(1);
coef(coef>kv.clim(2))=kv.clim(2);
end;
if flags.do_tc
xr=(-floor(N/2):floor((N-1)/2))*a;
else
xr=(0:N-1)*delta_t;
end;
if ~isempty(kv.fs)
xr=xr/kv.fs;
end;
switch(flags.plottype)
case 'image'
% Call imagesc explicitly with clim. This is necessary for the
% situations where the data (is by itself limited (from above or
% below) to within the specified range. Setting clim explicitly
% avoids the colormap moves in the top or bottom.
if isempty(kv.clim)
imagesc(xr,yr,coef);
else
imagesc(xr,yr,coef,kv.clim);
end;
case 'contour'
contour(xr,yr,coef);
case 'surf'
surf(xr,yr,coef,'EdgeColor','none');
case 'pcolor'
pcolor(xr,yr,coef);
end;
if flags.do_colorbar
colorbar;
end;
axis('xy');
if ~isempty(kv.fs)
xlabel(sprintf('%s (s)',kv.time),'fontsize',kv.fontsize);
else
xlabel(sprintf('%s (%s)',kv.time,kv.samples),'fontsize',kv.fontsize);
end;
if isempty(kv.fc)
ylabel('Channel No.','fontsize',kv.fontsize);
else
if isempty(kv.tick)
tickpos=linspace(1,M,kv.ntickpos);
tick=spline(1:M,kv.fc,tickpos);
set(gca,'YTick',tickpos);
set(gca,'YTickLabel',num2str(tick(:),3));
else
nlarge=1000;
tick=kv.tick;
% Create a crude inverse mapping to determine the positions of the
% ticks. Include half a channel in each direction, because it is
% possible to display a tick mark all the way to the edge of the
% plot.
manyticks=spline(1:M,kv.fc,linspace(0.5,M+0.5,nlarge));
% Keep only ticks <= than highest frequency+.5*bandwidth
tick=tick(tick<=manyticks(end));
% Keep only ticks >= lowest frequency-.5*bandwidth
tick=tick(tick>=manyticks(1));
nticks=length(tick);
tickpos=zeros(nticks,1);
for ii=1:nticks
jj=find(manyticks>=tick(ii));
tickpos(ii)=jj(1)/nlarge*M;
end;
set(gca,'YTick',tickpos);
set(gca,'YTickLabel',num2str(tick(:)));
end;
ylabel(sprintf('%s (Hz)',kv.frequency),'fontsize',kv.fontsize);
end;