-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathmyspectrogram.m
More file actions
100 lines (82 loc) · 4.09 KB
/
myspectrogram.m
File metadata and controls
100 lines (82 loc) · 4.09 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
%______________________________________________________________________________________________________________________
%
% @file myspectrogram.m
% @author Kamil Wojcicki
% @date April 2007
% @revision 002
% @brief Wrapper for Matlab's spectrogram function
%______________________________________________________________________________________________________________________
%
% @inputs s - speech signal
% fs - sampling frequency
% nfft - fft analysis length
% T - vector of frame width and frame shift (ms), i.e. [Tw, Ts]
% w - analysis window handle
% Slim - vector of spectrogram limits (dB), i.e. [Smin Smax]
% alpha - fir pre-emphasis filter coefficients
% cmap - color map
% cbar - color bar (boolean)
%
% @output handle - plot handle
%______________________________________________________________________________________________________________________
%
% @usage [handle] = myspectrogram(s, fs, nfft, T, w, Slim, alpha, cmap, cbar);
% [handle] = myspectrogram(s, 8000, 1024, [18,1], @hamming, [-45 -2], false, 'default', false);
% [handle] = myspectrogram(s, 8000, 1024, [18,1], @hamming, [-45 -2], [1 -0.97], 'default', true);
%______________________________________________________________________________________________________________________
function [handle] = myspectrogram(s, fs, nfft, T, w, Slim, alpha, cmap, cbar)
%__________________________________________________________________________________________________________________
% VALIDATE INPUTS
switch nargin
case 1, cbar=false; cmap='default'; alpha=false; Slim=[-45,-2]; w=@hamming; T=[18,1]; nfft=1024; fs=8000;
case 2, cbar=false; cmap='default'; alpha=false; Slim=[-45,-2]; w=@hamming; T=[18,1]; nfft=1024;
case 3, cbar=false; cmap='default'; alpha=false; Slim=[-45,-2]; w=@hamming; T=[18,1];
case 4, cbar=false; cmap='default'; alpha=false; Slim=[-45,-2]; w=@hamming;
case 5, cbar=false; cmap='default'; alpha=false; Slim=[-45,-2];
case 6, cbar=false; cmap='default'; alpha=false;
case 7, cbar=false; cmap='default';
case 8, cbar=false;
case 9
otherwise, error('Invalid number of input arguments.');
end
%__________________________________________________________________________________________________________________
% VARIABLES
Tw = T(1);
Ts = T(2);
Nw = round(fs * Tw * 0.001);
Ns = round(fs * Ts * 0.001);
N = length(s);
Smin = Slim(1);
Smax = Slim(2);
if(isstr(w)), w = str2func(w); end;
%__________________________________________________________________________________________________________________
% PRE PROCESS SPEECH
if(islogical(alpha) && alpha), s = filter([1 -0.97],1,s);
elseif(~islogical(alpha)) s = filter(alpha,1,s); end;
%__________________________________________________________________________________________________________________
% GET SPECTROGRAM DATA
[S,F,T] = spectrogram(s,w(Nw).',Nw-Ns,nfft,fs);
%[S,F,T] = specgram(s,nfft,fs,w(Nw).',Nw-Ns); % depreciated
%__________________________________________________________________________________________________________________
% SET DYNAMIC RANGE
S = abs(S);
S(S==0) = eps;
S = 20*log10(S);
S = S-max(max(S));
%__________________________________________________________________________________________________________________
% PLOT RESULTS
imagesc(T,F,S,[Smin Smax]);
axis('xy'); axis([0 N/fs 0 fs/2]);
xlabel('time (s)'); ylabel('frequency (Hz)');
set(gca,'YDir','normal');
if(cbar), colorbar; end
switch(lower(cmap))
case {'default','speech',''}
colormap('gray');
map=colormap;
colormap(1-map);
otherwise, colormap(cmap);
end
handle = gca;
%______________________________________________________________________________________________________________________
% EOF