-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathftgram.m
executable file
·327 lines (264 loc) · 9.15 KB
/
ftgram.m
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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
function [X, ax, cb, wx] = ftgram(x, fs, typename, varargin)
% FTGRAM - compute, plot short-time Fourier rransform
%
% X = ftgram(x, FS, TYPE, VARARGIN) returns X, the short-time Fourier
% transform of the input x, computed using stft(). The STFT is plotted using
% the sampling rate FS in Hz according to the string TYPE and parameters
% specified in name, value pairs. The variable TYPE can be 'rir', 'music' or
% 'speech'; it sets the defaults for the spectrogram and plotting parameters
% described below.
%
% NAME = [RIR_DEFAULT MUSIC_DEFAULT SPEECH_DEFAULT]; %% DESCRIPTION, UNITS
%
% STFT parameters
% 'nbins' = [512 2048 512]; %% dft half length, bins
% 'nskip' = nbins/2; %% stft hop size, samples
%
% spectrogram image axes
% 'dbrange' = [80 60 60]; %% gram dynamic range, dB
% 'logf' = [true true false]; %% logarithmic frequency axis, indicator
% 'logt' = [true false false]; %% logarithmic time axis, indicator
% 'ms' = [true false false]; %% time/frequency axis ms/kHz scaling, indicator
%
% waveform onset trimming
% 'trim' = [true false false]; %% trim waveform onset, indicator
% 'preroll' = 10; %% onset zeropad duration, milliseconds
% 'onsetlevel' = 1e-2; %% onset level, fraction
%
% waveform plot parameters
% 'waveform' = [true false false]; %% plot waveform, indicator
% 'tanhflag' = [true false false]; %% hyperbolic tangent saturation, indicator
% 'tanhbeta' = 5; %% hyperbolic tangent saturation parameter, ratio
%
% [~, AX, CB, WX] = ftgram() returns AX, the plot axes, one for each spectrogram
% channel, the first being the waveform plot, if present, and CB, the associated
% color bars, and WX, the waveform lines handle.
%
% See Also: STFT, IRGRAM.
% Created: 2-Feb-2011.
% Revised: 2-Feb-2011, MJW w/JSA, v1.
% Revised: 14-Mar-2012, JSA, v2 - interface, windowing reworked.
% Revised: 11-Jun-2012, JSA, v3 - nbins default changed for 'music' setting.
% Revised: 22-Aug-2013, JSA, v4 - seismic frequency range flag added.
% Revised: 8-Feb-2015, JSA, v5 - colorbar handles returned.
% Version: v5.
%% initialization, parse input
% initialize type defaults
nbins_default = [512 2048 512]; %% dft half length, bins
% nskip_default = nbins/2; %% stft hop size, samples
dbrange_default = [80 60 60]; %% gram dynamic range, dB
logf_default = [true true false]; %% logarithmic frequency axis, indicator
logt_default = [true false false]; %% logarithmic time axis, indicator
logtmin_default = [10 10 10]; %% logarithmic time axis offset, milliseconds
ms_default = [true false false]; %% time/frequency axis ms/kHz scaling, indicator
seismic_default = false; %% seismic frequency axis, indicator
trim_default = [true false false]; %% trim waveform onset, indicator
preroll_default = 10*[1 1 1]; %% onset zeropad duration, milliseconds
onsetlevel_default = 1e-2*[1 1 1]; %% onset level, fraction
waveform_default = [true false false]; %% plot waveform, indicator
tanhflag_default = [true false false]; %% hyperbolic tangent saturation, indicator
tanhbeta_default = 2*[1 1 1]; %% hyperbolic tangent saturation parameter, ratio
% set signal type
switch typename,
case 'rir',
% room impulse response
type = 1;
case 'music',
% music input
type = 2;
case 'speech',
% speech input
type = 3;
otherwise,
% music default
type = 2;
end;
% parse input
p = inputParser;
% waveform, sampling rate
p.addRequired('x', @(x)isnumeric(x)); %% waveform, signal matrix
p.addRequired('fs', @(x)isnumeric(x) && x>0); %% sampling rate, Hz
p.addRequired('typename', @(x)ischar(x)); %% sampling rate, Hz
% stft parameters
p.addParamValue('nbins', nbins_default(type), @(x)isnumeric(x));
p.addParamValue('nskip', 0, @(x)isnumeric(x));
% spectrogram image axes
p.addParamValue('dbrange', dbrange_default(type), @(x)isnumeric(x));
p.addParamValue('logf', logf_default(type), @(x)islogical(x));
p.addParamValue('logt', logt_default(type), @(x)islogical(x));
p.addParamValue('logtmin', logtmin_default(type), @(x)isnumeric(x));
p.addParamValue('ms', ms_default(type), @(x)islogical(x));
p.addParamValue('seismic', seismic_default, @(x)islogical(x));
% waveform onset trimming
p.addParamValue('trim', trim_default(type), @(x)islogical(x));
p.addParamValue('preroll', preroll_default(type), @(x)isnumeric(x));
p.addParamValue('onsetlevel', onsetlevel_default(type), @(x)isnumeric(x));
% waveform plot parameters
p.addParamValue('waveform', waveform_default(type), @(x)islogical(x));
p.addParamValue('tanhflag', tanhflag_default(type), @(x)islogical(x));
p.addParamValue('tanhbeta', tanhbeta_default(type), @(x)isnumeric(x));
p.parse(x, fs, typename, varargin{:});
% assign variables
nbins = p.Results.nbins;
if (p.Results.nskip > 0);
nskip = p.Results.nskip;
else,
nskip = nbins/2;
end;
dbrange = p.Results.dbrange;
logf = p.Results.logf;
logt = p.Results.logt;
logtmin = p.Results.logtmin;
ms = p.Results.ms;
seismic = p.Results.seismic;
trim = p.Results.trim;
preroll = p.Results.preroll;
onsetlevel = p.Results.onsetlevel;
waveform = p.Results.waveform;
tanhflag = p.Results.tanhflag;
beta = p.Results.tanhbeta;
%% condition input, form spectrogram
% find input signal size
nsamp = size(x,1);
if (nsamp == 1),
% make x a column
x = x(:);
[nsamp, nc] = size(x);
end;
[nsamp, nc] = size(x); %% signal length, samples; channel count, channels
% trim waveform onset
if trim,
istart = find(mean(abs(x)/max(max(abs(x))),2) > onsetlevel, 1) - round(preroll*fs/1000);
x = x(max(1,istart):end,:);
end;
nsamp = size(x,1);
% compute, normalize spectrogram
X = stft(x, nbins, nskip);
nframes = size(X,2)/nc;
y = x/max(max(abs(x)));
Y = 20*log10(abs(X)/max(max(abs(X)))+eps);
%% plot waveform
if waveform,
% define axis
figure(gcf);
ax = subplot(max(nc+1,3),1,1);
% tanh scaling
if tanhflag,
y = tanh(beta*y)/tanh(beta);
end;
% define time axis
t = [0:nsamp-1]/fs;
tscale = (~ms)*1 + ms*1000;
% plot waveform, label time axes
if logt,
wx = semilogx(tscale*(t+logtmin/1000), y); grid;
xlim(tscale*[logtmin/1000 logtmin/1000+nsamp/fs]);
n = get(gca,'Xtick');
set(gca,'XTickLabel',sprintf('%g |', n'));
else,
plot(tscale*t, y); grid;
xlim(tscale*[0 nsamp/fs]);
end;
% y-axis labels
if tanhflag,
ylabel('Amplitude (dB)');
set(gca,'YTick', sort(kron([-1 1], tanh(10.^-([0:10:30]/20)*beta))));
set(gca,'YTickLabel',[' 0'; '-10'; '-20'; '-30'; '-30'; '-20'; '-10'; ' 0'])
ylim(tanh(10^(1/20)*beta)*[-1 1]);
else,
ylim([-1 1]);
ylabel('Amplitude');
end;
end;
%% plot spectrogram
% define time, frequency, energy axes
tscale = (~ms)*1 + ms*1000;
np = ceil(nbins/(2*nskip));
nq = ceil((nsamp-(nframes-1)*nskip-nbins/2)/nskip);
t = tscale * ([-np:nframes-1+nq]*nskip + nbins/2)/fs;
f = 1/tscale * fs/2*[0:nbins]/nbins;
f(1) = eps;
cb = zeros(1,nc);
% loop through specgtrograms
for s = [1:nc],
% get axes
if (nc == 1),
if waveform,
ax(2) = subplot(3,1,[2 3]);
else,
ax(1) = subplot(1,1,1);
end;
else,
ax(waveform+s) = subplot(nc+waveform,1,s+waveform);
end;
% display spectrogram
offset = logt * tscale*logtmin/1000;
surf(t+offset, f, Y(1:nbins+1, (s-1)*nframes + [ones(1,np) [1:nframes] nframes*ones(1,nq)]), 'edgecolor', 'none');
axis tight;
view(0,90);
% time, frequency scaling
if ms,
if (s == nc),
xlabel('Time (ms)');
end;
ylabel('Frequency (kHz)');
else,
if (s == nc),
xlabel('Time (s)');
end;
ylabel('Frequency (Hz)');
end;
% scale, label frequency axis
if logf,
set(gca, 'yScale', 'log');
end;
% scale, label time ax1s
if logt,
set(gca, 'xScale', 'log');
xlim(tscale*[logtmin/1000 logtmin/1000+nsamp/fs]);
n = get(gca,'Xtick');
set(gca,'XTickLabel',sprintf('%g |', n'));
end;
% scale, label frequency axis
if logf,
if seismic,
% seismic frequency axis
divs = [10 20 50 100 200 500 1000]/tscale;
set(gca, 'ytickmode', 'manual');
set(gca, 'ytick', divs);
ylim([min(divs) min(fs/2,max(divs))]);
else,
% audio frequency axis
divs = [20 50 100 200 500 1000 2000 5000 10000 20000]/tscale;
set(gca, 'ytickmode', 'manual');
set(gca, 'ytick', divs);
ylim([min(divs) max(divs)]);
end;
else,
if seismic,
% seismic frequency axis
ylim([0 min(fs/2,1000)]/tscale);
else,
% audio frequency axis
ylim([0 20000]/tscale);
end;
end;
% display color bar
caxis([-dbrange 0])
cb(s) = colorbar();
if waveform*(nc == 1),
set(cb(s), 'Position', [0.916 0.11 0.015 0.517]);
elseif (nc == 1),
set(cb(s), 'Position', [0.916 0.11 0.015 0.815]);
else,
temp = get(cb(s), 'Position');
set(cb(s), 'Position', [0.916 temp(2)+0.004 0.015 temp(4)]);
end;
colormap(jet);
ylabel(cb(s),'Energy (dB)');
end;
% link x-axes of plots:
if (waveform + nc-1),
linkaxes(ax,'x');
end;
end