ReSpeaker DSP V02

Dependencies:   mbed MbedJSONValue

Matlab_Script/ArbMagFilter.txt

Committer:
Arkadi
Date:
2019-06-20
Revision:
14:8a4699aa69b5
Parent:
1:574b54755983

File content as of revision 14:8a4699aa69b5:

% Arbitrary Magnitude Filter test on real WAV files
% Sampling frequency from pin D12
% For board F446RE
% Fd12 = 402 * 2;  % KHz; for 4 sections
% Fd12 = 308 * 2;  % KHz; for 6 sections
Fd12 = 250 * 2;  % KHz; for 8 sections


%--------------------------------
% For old board
% Fd12 = 400 * 2;  % KHz; for one section
% Fd12 = 290 * 2;  % KHz; for two sections
% Fd12 = 190 * 2;  % KHz; for four sections
% Fd12 = 142 * 2;  % KHz; for six sections

% For iirlpnorm filter description see http://www.mathworks.com/help/dsp/ref/iirlpnorm.html

Nb = 16;   % Numerator Order
Na = 16;   % Denominator Order
P    = [2 128];        % P'th norm
dens = 20;             % Density Factor


% Frequency Vector - real, Hz
Freal = [0 5 10 15 20 25 30 35 40 45 50 55 60 65 70 75 80 85 90 95 100 105 110 115 120 125 130 135 140 Fd12/2];
% Amplitudas calculation
[A, dBDiff] = AmplCalc(length(Freal));
% Frequency Vector - normalized frequences =   Freal / (Fd12/2)  wher Fd12 is a board sampling frequency
% used as a parameter for filter calculation
Fnormal = Freal / (Fd12/2);
% Weights has one entry per frequency point (the same length as F and A) which tells iirlpnorm how 
% much emphasis to put on minimizing the error in the vicinity of each frequency point relative to the other points.
W    = [ones(1,30)];  
% Frequency Edges, normalized (single band)
E    = [0 1];

% Hints
% 
% This is a weighted least-pth optimization.
% Check the radii and locations of the poles and zeros for your filter (with zplane function). If the zeros are on the unit circle 
% and the poles are well inside the unit circle, try increasing the order of the numerator or reducing the error weighting in the stopband.
% Similarly, if several poles have a large radii and the zeros are well inside of the unit circle, try increasing the order 
% of the denominator or reducing the error weighting in the passband.

% Analysis parameters
TestFilter = true;  % test filter on WAV file
AmplitudeAnalysis = true;
FrequencyAnalysis = true;
SpectrogramAnalysis = true;

[num,den,err,SOS,G] = iirlpnorm(Nb, Na, Fnormal, E, A, W, P, {dens});

MyArbMagFilter = dfilt.df1sos(SOS, G);

% [MyArbMagFilter,SOS, G] = ArbMagFilterDesign(4, 4);
fvtool(MyArbMagFilter, 'Fs', Fd12*1000);

if (TestFilter) % test filter on WAV file
    % ybat - samples
    % Fs - sampling frequency
    
    % this line should be corrected depended on location of WAV file
    mainfilename ='C:\Users\Igor\OneDrive\dev\Bats\tests\kuhlii2';
    [ybat,Fs] = audioread(strcat(mainfilename,'.wav'));
    % [ybat,Fs] = audioread('C:\Users\Igor\OneDrive\dev\Bats\tests\sasha_white.wav');

    fprintf('Amount of samples: %d; Sampling frequency %d\n',length(ybat), Fs)
    t = linspace(0,length(ybat)/Fs,length(ybat));  
    % Apply filter
    yf = filter(MyArbMagFilter,ybat);
    
    FilterResultsAnalysis(t, ybat, yf, Fs, AmplitudeAnalysis, FrequencyAnalysis, SpectrogramAnalysis);
    audiowrite(strcat(mainfilename,'-filtered.wav'), yf, Fs);
end

% Print filter in the form, suitable for copying into C code
columns = size(SOS,2);
lines = size(SOS,1);
fprintf('/*\nIIRLPNORM FILTER DESIGN, %s, %d sections \nFrequency ranges (for Fs=%5.1f*2 KHz), amplitudas and weights\nF ', datestr(now), lines, Fd12/2);
fprintf('%5.1f ', Freal)
fprintf('\n  ');
fprintf('%5.2f ', Fnormal)
fprintf('\nA ');
fprintf('%5.1f ', A);
fprintf('\nW ');
fprintf('%4.2f ', W);
fprintf('\n*/\n');
% fprintf('char FilterType[] = "IIRLPNORM";\n');
fprintf('float SOSMat[%d][%d] = {\n', lines, columns);
for i =1:lines
    fprintf(' %f, ', SOS(i,1:columns));
    fprintf('\n');
end
fprintf('\b\b\b\n};\n'); %\b\b\b  - erase <\n>, <space> and <,>
fprintf('float Gscale = %f;\n\n',G);