Demo program of digital signal processing: Variable LPF/HPF, Vocal canceller, Pitch shifter, Reverbrator. ディジタル信号処理のデモプログラム. 遮断周波数可変 LPF/HPF,ボーカルキャンセラ,ピッチシフタ,残響生成器.

Dependencies:   Array_Matrix F446_AD_DA UIT_AQM1602 mbed

Committer:
MikamiUitOpen
Date:
Tue Feb 21 13:14:47 2017 +0000
Revision:
6:96b4f6ce2dc9
Parent:
0:fa74b1130cc3
7

Who changed what in which revision?

UserRevisionLine numberNew contents of line
MikamiUitOpen 0:fa74b1130cc3 1 //------------------------------------------------------------------------
MikamiUitOpen 0:fa74b1130cc3 2 // Design of Butterworth LPF and HPF using bilinear transform
MikamiUitOpen 0:fa74b1130cc3 3 //
MikamiUitOpen 0:fa74b1130cc3 4 // 2016/03/31, Copyright (c) 2016 MIKAMI, Naoki
MikamiUitOpen 0:fa74b1130cc3 5 //------------------------------------------------------------------------
MikamiUitOpen 0:fa74b1130cc3 6
MikamiUitOpen 0:fa74b1130cc3 7 #include "BilinearDesignLH.hpp"
MikamiUitOpen 0:fa74b1130cc3 8
MikamiUitOpen 0:fa74b1130cc3 9 namespace Mikami
MikamiUitOpen 0:fa74b1130cc3 10 {
MikamiUitOpen 0:fa74b1130cc3 11 // Execute design
MikamiUitOpen 0:fa74b1130cc3 12 // input
MikamiUitOpen 0:fa74b1130cc3 13 // fc: Cutoff frequency
MikamiUitOpen 0:fa74b1130cc3 14 // pb: Passband (LPF or HPF)
MikamiUitOpen 0:fa74b1130cc3 15 // output
MikamiUitOpen 0:fa74b1130cc3 16 // c : Coefficients for cascade structure
MikamiUitOpen 0:fa74b1130cc3 17 // g : Gain factor for cascade structure
MikamiUitOpen 0:fa74b1130cc3 18 void BilinearDesign::Execute(float fc, Type pb, Coefs c[], float& g)
MikamiUitOpen 0:fa74b1130cc3 19 {
MikamiUitOpen 0:fa74b1130cc3 20 Butterworth();
MikamiUitOpen 0:fa74b1130cc3 21 Bilinear(fc);
MikamiUitOpen 0:fa74b1130cc3 22 ToCascade(pb);
MikamiUitOpen 0:fa74b1130cc3 23 GetGain(pb);
MikamiUitOpen 0:fa74b1130cc3 24 GetCoefs(c, g);
MikamiUitOpen 0:fa74b1130cc3 25 }
MikamiUitOpen 0:fa74b1130cc3 26
MikamiUitOpen 0:fa74b1130cc3 27 // Get poles for Butterworth characteristics
MikamiUitOpen 0:fa74b1130cc3 28 void BilinearDesign::Butterworth()
MikamiUitOpen 0:fa74b1130cc3 29 {
MikamiUitOpen 0:fa74b1130cc3 30 float pi_2order = PI_/(2.0f*ORDER_);
MikamiUitOpen 0:fa74b1130cc3 31 for (int j=0; j<ORDER_/2; j++) // Pole with imaginary part >= 0
MikamiUitOpen 0:fa74b1130cc3 32 {
MikamiUitOpen 0:fa74b1130cc3 33 float theta = (2.0f*j + 1.0f)*pi_2order;
MikamiUitOpen 0:fa74b1130cc3 34 sP_[j] = Complex(-cosf(theta), sinf(theta));
MikamiUitOpen 0:fa74b1130cc3 35 }
MikamiUitOpen 0:fa74b1130cc3 36 }
MikamiUitOpen 0:fa74b1130cc3 37
MikamiUitOpen 0:fa74b1130cc3 38 // Bilinear transform
MikamiUitOpen 0:fa74b1130cc3 39 // fc: Cutoff frequency
MikamiUitOpen 0:fa74b1130cc3 40 void BilinearDesign::Bilinear(float fc)
MikamiUitOpen 0:fa74b1130cc3 41 {
MikamiUitOpen 0:fa74b1130cc3 42 float wc = tanf(fc*PI_FS_);
MikamiUitOpen 0:fa74b1130cc3 43 for (int k=0; k<ORDER_/2; k++)
MikamiUitOpen 0:fa74b1130cc3 44 zP_[k] = (1.0f + wc*sP_[k])/(1.0f - wc*sP_[k]);
MikamiUitOpen 0:fa74b1130cc3 45 }
MikamiUitOpen 0:fa74b1130cc3 46
MikamiUitOpen 0:fa74b1130cc3 47 // Convert to coefficients for cascade structure
MikamiUitOpen 0:fa74b1130cc3 48 void BilinearDesign::ToCascade(Type pb)
MikamiUitOpen 0:fa74b1130cc3 49 {
MikamiUitOpen 0:fa74b1130cc3 50 for (int j=0; j<ORDER_/2; j++)
MikamiUitOpen 0:fa74b1130cc3 51 {
MikamiUitOpen 0:fa74b1130cc3 52 ck_[j].a1 = 2.0f*real(zP_[j]); // a1m
MikamiUitOpen 0:fa74b1130cc3 53 ck_[j].a2 = -norm(zP_[j]); // a2m
MikamiUitOpen 0:fa74b1130cc3 54 ck_[j].b1 = (pb == LPF) ? 2.0f : -2.0f; // b1m
MikamiUitOpen 0:fa74b1130cc3 55 ck_[j].b2 = 1.0f; // b2m
MikamiUitOpen 0:fa74b1130cc3 56 }
MikamiUitOpen 0:fa74b1130cc3 57 }
MikamiUitOpen 0:fa74b1130cc3 58
MikamiUitOpen 0:fa74b1130cc3 59 // Calculate gain factor
MikamiUitOpen 0:fa74b1130cc3 60 void BilinearDesign::GetGain(Type pb)
MikamiUitOpen 0:fa74b1130cc3 61 {
MikamiUitOpen 0:fa74b1130cc3 62 float u = (pb == LPF) ? 1.0f : -1.0f;
MikamiUitOpen 0:fa74b1130cc3 63 float g0 = 1.0f;
MikamiUitOpen 0:fa74b1130cc3 64 for (int k=0; k<ORDER_/2; k++)
MikamiUitOpen 0:fa74b1130cc3 65 g0 = g0*(1.0f - (ck_[k].a1 + ck_[k].a2*u)*u)/
MikamiUitOpen 0:fa74b1130cc3 66 (1.0f + (ck_[k].b1 + ck_[k].b2*u)*u);
MikamiUitOpen 0:fa74b1130cc3 67 gain_ = g0;
MikamiUitOpen 0:fa74b1130cc3 68 }
MikamiUitOpen 0:fa74b1130cc3 69
MikamiUitOpen 0:fa74b1130cc3 70 // Get coefficients
MikamiUitOpen 0:fa74b1130cc3 71 void BilinearDesign::GetCoefs(Coefs c[], float& gain)
MikamiUitOpen 0:fa74b1130cc3 72 {
MikamiUitOpen 0:fa74b1130cc3 73 for (int k=0; k<ORDER_/2; k++) c[k] = ck_[k];
MikamiUitOpen 0:fa74b1130cc3 74 gain = gain_;
MikamiUitOpen 0:fa74b1130cc3 75 }
MikamiUitOpen 0:fa74b1130cc3 76 }