-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmel.m
138 lines (128 loc) · 4.57 KB
/
mel.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
function varargout = mel(options)
% librosa.mel Design Mel filter bank
%
% This function matches the mel function from Librosa (tested for
% version 0.10.1). Parameter defaults are identical to the Librosa
% function.
%
% FB = librosa.mel(SampleRate=fs) returns a frequency-domain
% Mel filter bank, filterBank. fs is the input sample rate, in Hz.
%
% FB = librosa.mel(FFTLength=NFFT) specifies the FFT length.
%
% FB = librosa.mel(NumBands=NUMBANDS) specifies the number of bands in
% the filter bank.
%
% FB = librosa.mel(Fmin=FMIN) specifies the lowest frequency (in Hz).
%
% FB = librosa.mel(Fmax=FMAX) specifies the highest frequency (in Hz).
%
% FB = librosa.mel(Normalization=NORM) specifies the type of filter bank
% normalization.
%
% FB = librosa.mel(HTK=FLAG) specifies what type of Mel scaling is used.
% If FLAG is true, HTK scaling is used. If FLAG is false, Slaney scaling
% is used. The default is Slaney-scaling.
%
% FB = librosa.mel(GenerateMATLABCode=true) generates and opens an
% untitled file containing code that implements the code of librosa.mel
% using the MATLAB function designAuditoryFilterBank.
%
% % Example:
% % Design an auditory filter bank and use it to compute a mel
% % spectrogram.
%
% [audioIn,fs] = audioread("Counting-16-44p1-mono-15secs.wav");
%
% % Compute spectrogram
% win = hann(1024,"periodic");
% [~,F,T,S] = spectrogram(audioIn,win,512,1024,fs,"onesided");
%
% % Design auditory filter bank
% filterBank = librosa.mel(SampleRate=fs,FFTLength=1024, ...
% NumBands=16,Normalization="None",HTK=true);
%
% % Visualize filter bank
% plot(F,filterBank.')
% grid on
% title("Mel Filter Bank")
% xlabel("Frequency (Hz)")
%
% % Compute mel spectrogram
% SMel = filterBank*S;
% Copyright 2022-2023 The MathWorks, Inc.
%% Parse function parameters
arguments
options.FFTLength (1,1) double {mustBeNumeric,mustBePositive,mustBeInteger} = 2048
options.SampleRate (1,1) double {mustBeNumeric,mustBePositive} = 22050
options.NumBands (1,1) double {mustBeNumeric,mustBePositive,mustBeInteger} = 128
options.Fmin (1,1) double {mustBeNumeric,mustBeNonnegative} = 0
options.Fmax (1,1) double {mustBeNumeric,mustBePositive}
options.HTK (1,1) logical {mustBeNumericOrLogical} = false
options.Normalization = 'Slaney'
options.GenerateMATLABCode (1,1) logical {mustBeNumericOrLogical} = false
end
if ~isfield(options,'Fmax')
options.Fmax = options.SampleRate/2;
else
options.Fmax = min(options.SampleRate/2,options.Fmax);
end
switch options.Normalization
case 'None'
options.Normalization = 'none';
customNorm = false;
case 'Slaney'
options.Normalization = 'bandwidth';
customNorm = false;
otherwise
options.Normalization = 'none';
customNorm = true;
mustBeNumeric(options.Normalization)
end
if options.HTK
melStyle = "oshaughnessy";
else
melStyle = "slaney";
end
if options.GenerateMATLABCode
strWriter = StringWriter;
else
strWriter = librosa.utils.StringWriter;
end
strWriter.addcr('%s\n%% Construct filter bank.','%%');
filterBank = designAuditoryFilterBank(options.SampleRate, ...
FrequencyScale="mel", ...
FFTLength=options.FFTLength, ...
NumBands=options.NumBands, ...
FrequencyRange=[options.Fmin options.Fmax], ...
Normalization=options.Normalization, ...
OneSided=true, ...
FilterBankDesignDomain="linear", ...
MelStyle=melStyle);
strWriter.addcr('filterBank = designAuditoryFilterBank(%f,...',options.SampleRate);
strWriter.addcr('FrequencyScale="mel",...');
strWriter.addcr('FFTLength=%d,...',options.FFTLength);
strWriter.addcr('NumBands=%d,...',options.NumBands);
strWriter.addcr('FrequencyRange=[%f %f],...',options.Fmin,options.Fmax);
strWriter.addcr('Normalization=''%s'',...',options.Normalization);
strWriter.addcr('OneSided=true,...');
strWriter.addcr('MelStyle=''%s'',...',melStyle);
strWriter.addcr('FilterBankDesignDomain="linear");');
if customNorm
n = options.Normalization;
strWriter.addcr('%s\n%% Normalize filter bank.','%%');
if ~isequal(n,-Inf)
val = vecnorm(filterBank,n,2);
strWriter.addcr('n = vecnorm(filterBank,%f,2);',options.Normalization);
filterBank = filterBank./val;
strWriter.addcr('filterBank = filterBank./n;');
end
end
varargout{1} = filterBank;
varargout{2} = strWriter.char;
if options.GenerateMATLABCode
footer = sprintf('%% _Generated by MATLAB (R) and Audio Toolbox on %s_',string(datetime("now")));
strWriter.addcr('\n%s\n%s','%%',footer);
matlab.internal.liveeditor.openAsLiveCode(strWriter.char)
end
end