1+ function out = generateMelSpectrogramCode(args ,type )
2+ % generateMelSpectrogramCode Generate MATLAB code to perform Mel
3+ % spectrogram computation
4+
5+ % Copyright 2022 The MathWorks, Inc.
6+
7+ if nargin == 1
8+ type= ' object' ;
9+ end
10+
11+ center = args{" center" };
12+ if center
13+ error(' center must be false to get a match' )
14+ end
15+
16+ htk = args{" htk" };
17+ if ~htk
18+ error(' htk must be true to get a match' )
19+ end
20+
21+ sr = args{" sr" };
22+ sr = double(sr );
23+
24+ windowType = args{" window" };
25+ windowType = char(windowType );
26+
27+ WindowLength = args{" win_length" };
28+ WindowLength = double(WindowLength );
29+
30+ HopLength = args{" hop_length" };
31+ HopLength = double(HopLength );
32+
33+ FFTLength = args{" n_fft" };
34+ FFTLength = double(FFTLength );
35+
36+ n_mels = args{" n_mels" };
37+ n_mels = double(n_mels );
38+
39+ norm = args{" norm" };
40+ norm = char(norm );
41+
42+ fmin = args{" fmin" };
43+ fmin = double(fmin );
44+
45+ fmax = args{" fmax" };
46+ fmax = double(fmax );
47+
48+ switch norm
49+ case ' None'
50+ norm = ' none' ;
51+ case ' slaney'
52+ norm = ' bandwidth' ;
53+ otherwise
54+ norm = ' area' ;
55+ end
56+
57+ code = sprintf(' afe = audioFeatureExtractor(SampleRate=%f ,Window=%s (%d ,"periodic"),...\n OverlapLength=%d ,FFTLength=%d ,melSpectrum=true);\n ' ,sr ,windowType ,WindowLength ,WindowLength - HopLength ,FFTLength );
58+ code = sprintf(' %s\n setExtractorParameters(afe,"melSpectrum",SpectrumType="power",...\n ' ,code );
59+ code = sprintf(' %s FilterBankDesignDomain="linear",...\n ' ,code );
60+ code = sprintf(' %s FilterBankNormalization="%s ",...\n ' ,code ,norm );
61+ code = sprintf(' %s WindowNormalization=false,...\n ' ,code );
62+ code = sprintf(' %s NumBands=%d ,...);\n ' ,code ,n_mels );
63+ code = sprintf(' %s FrequencyRange=[%f %f ]);\n ' ,code ,fmin ,fmax );
64+
65+ if strcmp(type ,' object' )
66+ eval(code );
67+ out = afe ;
68+ elseif strcmp(type ,' block' )
69+ open_system(new_system )
70+ name = get_param(gcs ,' Name' );
71+ blk = [name ' /Mel Spectrogram' ];
72+ add_block(' audiofeatures/Mel Spectrogram' ,blk )
73+ set_param(blk ,' NumBands' ,num2str(n_mels ))
74+ set_param(blk ,' FilterBankNormalization' ,norm )
75+ set_param(blk ,' OverlapLength' ,num2str(WindowLength - HopLength ))
76+ set_param(blk ,' WindowParameter' ,sprintf(' %s (%d ,"periodic")' ,windowType ,WindowLength ))
77+ set_param(blk ,' AutoFFTLength' ,' off' )
78+ set_param(blk ,' FFTLength' ,num2str(FFTLength ))
79+ set_param(blk ,' SampleRate' ,num2str(sr ))
80+ set_param(blk ,' SpectrumType' ,' power' )
81+ set_param(blk ,' WindowNormalization' ,' off' )
82+ set_param(blk ,' FrequencyRange' ,sprintf(' [%f %f ]' ,fmin ,fmax ))
83+ set_param(blk ,' AutoFrequencyRange' ,' off' )
84+ % Set parameters
85+ out = gcs ;
86+ else
87+ out = sprintf(' %s\n S = extract(afe,y);' ,code );
88+ end
89+
90+ end
0 commit comments