ita_dsp_design_filters.m 3.19 KB
Newer Older
Dipl.-Ing. Jonas Stienen's avatar
Dipl.-Ing. Jonas Stienen committed
1
%% Settings
2
Fs = 44100;
Dipl.-Ing. Jonas Stienen's avatar
Dipl.-Ing. Jonas Stienen committed
3
4
N = 10;
freqs = ita_ANSI_center_frequencies;
5
6
7

%% Export to C/C++ header using std::vector

Dipl.-Ing. Jonas Stienen's avatar
Dipl.-Ing. Jonas Stienen committed
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
target_file = 'ITADSPThirdOctaveFilterbankIIRCoefficients.h';
proto_file = 'ITADSPThirdOctaveFilterbankCoefficients.proto.h';
copyfile( proto_file, target_file );

fd = fopen( target_file, 'r' );
stub_cpp_file_body = textscan( fd, '%s', 'delimiter', '\n' );
fclose( fd );

fd = fopen( target_file, 'wt' );
for i=1:size( stub_cpp_file_body{ 1 } , 1 )
    if strcmp( '### stub code ###',  stub_cpp_file_body{ 1 }{ i } ) 
        break;
    end
    fprintf( fd, '%s\n', stub_cpp_file_body{ 1 }{ i } );
end

fprintf( fd, '/* This file has been generated by the ita_dsp_design_filsters.m Matlab script */\n\n' );

fprintf( fd, '#ifndef IW_ITA_DSP_THIRD_OCTAVE_FILTERBANK_IIR_COEFFICIENTS\n' );
fprintf( fd, '#define IW_ITA_DSP_THIRD_OCTAVE_FILTERBANK_IIR_COEFFICIENTS\n\n' );
fprintf( fd, '#include <vector>\n\n' );

fprintf( fd, 'namespace ITADSPThirdOctaveFilterbankIIR\n{\n\n' );
fprintf( fd, 'const double dSamplingFrequency = %0.2f; // Hz\n', Fs );
fprintf( fd, 'const int iOrdner = %d;\n', N );
fprintf( fd, 'const int iNumFrequencies = %d;\n', numel( freqs ) );
fprintf( fd, 'const std::vector< float > vfFrequencies =\n{\n' );
for l=1:numel( freqs )
    fprintf( fd, '%.1ff, // Hz\n', freqs( l ) );
end
fprintf( fd, '};\n' );
fprintf( fd, '\n' );

%% Filter design

fprintf( fd, '// 3-dim vector access: FrequencyBand ; Biquad ; Param = { ScaleValue [1], sosMatrix [2-6] }\n' );
fprintf( fd, 'const std::vector< std::vector< std::vector< float > > > vvvfSOSMatrixCoefficients = \n{\n\n' );

% Lowpass
h  = fdesign.lowpass( 'N,F3dB', N, freqs( 1 ), Fs );
Hd = design( h, 'butter' );

fprintf( fd, '{\n' );
fprintf( fd, '// Band %.1f Hz sos matrix\n', freqs( 1 ) );
for k=1:size( Hd.sosMatrix, 1 )
    fprintf( fd, '{ %.6ff, ', Hd.ScaleValues( k ) );
    for l=1:size( Hd.sosMatrix, 2 )
        fprintf( fd, '%.6ff, ', Hd.sosMatrix( k, l ) );
    end
    fprintf( fd, '},\n' );
end
fprintf( fd, '},\n\n' );

% Bandpasses
for n = 2:( numel( freqs ) - 1 )
    
    h  = fdesign.bandpass( 'N,F3dB1,F3dB2', N, freqs( n - 1 ), freqs( n + 1 ), Fs );
    Hd = design( h, 'butter' );

    fprintf( fd, '{\n' );
    fprintf( fd, '// Band %.1f Hz sos matrix\n', freqs( n ) );
    for k=1:size( Hd.sosMatrix, 1 )
        fprintf( fd, '{ %.6ff, ', Hd.ScaleValues( k ) );
        for l=1:size( Hd.sosMatrix, 2 )
            fprintf( fd, '%.6ff, ', Hd.sosMatrix( k, l ) );
        end
        fprintf( fd, '},\n' );
    end
    fprintf( fd, '},\n\n' );
    
end

% Highpass
h  = fdesign.highpass( 'N,F3dB', N, freqs( end ), Fs );
Hd = design( h, 'butter' );

fprintf( fd, '{\n' );
fprintf( fd, '// Band %.1f Hz sos matrix\n', freqs( end ) );
for k=1:size( Hd.sosMatrix, 1 )
	fprintf( fd, '{ %.6ff, ', Hd.ScaleValues( k ) );
    for l=1:size( Hd.sosMatrix, 2 )
        fprintf( fd, '%.6ff, ', Hd.sosMatrix( k, l ) );
    end
    fprintf( fd, '},\n' );
end
fprintf( fd, '},\n\n' );

fprintf( fd, '};\n\n' );

fprintf( fd, '} // namespace\n\n' );

fprintf( fd, '#endif // IW_ITA_DSP_THIRD_OCTAVE_FILTERBANK_IIR_COEFFICIENTS\n' );

%% Finish
for ii=(i+1):size( stub_cpp_file_body{ 1 } , 1 )
    fprintf( fd, '%s\n', stub_cpp_file_body{ 1 }{ ii } );
end
105
fclose( fd );