![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
Chapter006_Zara_F746NG
Dependencies: F746_GUI SRF08 Array_Matrix TS_DISCO_F746NG mbed LCD_DISCO_F746NG BSP_DISCO_F746NG SDFileSystem_Warning_Fixed FrequencyResponseDrawer F746_SAI_IO
MyClasses_Functions/DesignerDrawer.cpp@0:04b43b777fae, 2016-04-19 (annotated)
- Committer:
- MikamiUitOpen
- Date:
- Tue Apr 19 09:46:11 2016 +0000
- Revision:
- 0:04b43b777fae
- Child:
- 2:2478e7a8e8d5
1
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
MikamiUitOpen | 0:04b43b777fae | 1 | //------------------------------------------------------------------------------ |
MikamiUitOpen | 0:04b43b777fae | 2 | // IIR フィルタを双一次 z 変換で設計し,その周波数特性を描画するためのクラス |
MikamiUitOpen | 0:04b43b777fae | 3 | // |
MikamiUitOpen | 0:04b43b777fae | 4 | // 2016/04/19, Copyright (c) 2016 MIKAMI, Naoki |
MikamiUitOpen | 0:04b43b777fae | 5 | //------------------------------------------------------------------------------ |
MikamiUitOpen | 0:04b43b777fae | 6 | |
MikamiUitOpen | 0:04b43b777fae | 7 | #include "DesignerDrawer.hpp" |
MikamiUitOpen | 0:04b43b777fae | 8 | |
MikamiUitOpen | 0:04b43b777fae | 9 | namespace Mikami |
MikamiUitOpen | 0:04b43b777fae | 10 | { |
MikamiUitOpen | 0:04b43b777fae | 11 | // Constructor |
MikamiUitOpen | 0:04b43b777fae | 12 | DesignerDrawer::DesignerDrawer(uint16_t x0, uint16_t y0, |
MikamiUitOpen | 0:04b43b777fae | 13 | uint16_t db10, int fs, int order, |
MikamiUitOpen | 0:04b43b777fae | 14 | float fc, uint16_t fL, uint16_t fH, |
MikamiUitOpen | 0:04b43b777fae | 15 | BilinearDesign::Type lpHp) |
MikamiUitOpen | 0:04b43b777fae | 16 | : lcd_(GuiBase::GetLcdPtr()), ts_(GuiBase::GetTsPtr()), |
MikamiUitOpen | 0:04b43b777fae | 17 | X0_(x0), Y0_(y0), DB10_(db10), ORDER_(order), |
MikamiUitOpen | 0:04b43b777fae | 18 | CURSOR_Y0_(y0+1-db10*6), CURSOR_LENGTH_(db10*6-1), |
MikamiUitOpen | 0:04b43b777fae | 19 | LOWER_F_(fL), HIGHER_F_(fH), |
MikamiUitOpen | 0:04b43b777fae | 20 | CURSOR_COLOR_(0xFFE000D0), CURSOR_TOUCHED_COLOR_(0xFFFF80FF) |
MikamiUitOpen | 0:04b43b777fae | 21 | { |
MikamiUitOpen | 0:04b43b777fae | 22 | drawerObj_ = new FrqRespDrawer(x0, 100.0f, 20000.0f, 140, |
MikamiUitOpen | 0:04b43b777fae | 23 | y0, -60, 0, db10, fs); |
MikamiUitOpen | 0:04b43b777fae | 24 | |
MikamiUitOpen | 0:04b43b777fae | 25 | // 双一次 z 変換による IIR フィルタの設計 |
MikamiUitOpen | 0:04b43b777fae | 26 | designObj_ = new BilinearDesign(order, fs); |
MikamiUitOpen | 0:04b43b777fae | 27 | coefs_ = new BilinearDesign::Coefs[order/2]; |
MikamiUitOpen | 0:04b43b777fae | 28 | ck_ = (Biquad::Coefs *)coefs_; |
MikamiUitOpen | 0:04b43b777fae | 29 | |
MikamiUitOpen | 0:04b43b777fae | 30 | fC_ = fc; // 最初に与える遮断周波数 |
MikamiUitOpen | 0:04b43b777fae | 31 | designObj_->Execute(fC_, lpHp, coefs_, g0_); |
MikamiUitOpen | 0:04b43b777fae | 32 | frqResp_.SetParams(ORDER_, g0_, ck_); |
MikamiUitOpen | 0:04b43b777fae | 33 | |
MikamiUitOpen | 0:04b43b777fae | 34 | // 周波数特性の描画 |
MikamiUitOpen | 0:04b43b777fae | 35 | lblFrq_ = new NumericLabel<int>(110, 30); |
MikamiUitOpen | 0:04b43b777fae | 36 | DrawResponse(); |
MikamiUitOpen | 0:04b43b777fae | 37 | tp_ = new TouchPanelDetectorX( |
MikamiUitOpen | 0:04b43b777fae | 38 | drawerObj_->X(LOWER_F_), drawerObj_->X(HIGHER_F_), |
MikamiUitOpen | 0:04b43b777fae | 39 | CURSOR_Y0_, y0); |
MikamiUitOpen | 0:04b43b777fae | 40 | lp_ = lpHp; |
MikamiUitOpen | 0:04b43b777fae | 41 | cursorRedraw_ = false; |
MikamiUitOpen | 0:04b43b777fae | 42 | } |
MikamiUitOpen | 0:04b43b777fae | 43 | |
MikamiUitOpen | 0:04b43b777fae | 44 | DesignerDrawer::~DesignerDrawer() |
MikamiUitOpen | 0:04b43b777fae | 45 | { |
MikamiUitOpen | 0:04b43b777fae | 46 | delete tp_; |
MikamiUitOpen | 0:04b43b777fae | 47 | delete lblFrq_; |
MikamiUitOpen | 0:04b43b777fae | 48 | delete coefs_; |
MikamiUitOpen | 0:04b43b777fae | 49 | delete designObj_; |
MikamiUitOpen | 0:04b43b777fae | 50 | delete drawerObj_; |
MikamiUitOpen | 0:04b43b777fae | 51 | } |
MikamiUitOpen | 0:04b43b777fae | 52 | |
MikamiUitOpen | 0:04b43b777fae | 53 | // フィルタの再設計と周波数特性の再描画 |
MikamiUitOpen | 0:04b43b777fae | 54 | bool DesignerDrawer::ReDesignAndDraw(Biquad::Coefs *ck, float &g0, |
MikamiUitOpen | 0:04b43b777fae | 55 | BilinearDesign::Type lpHp) |
MikamiUitOpen | 0:04b43b777fae | 56 | { |
MikamiUitOpen | 0:04b43b777fae | 57 | bool changed = (lpHp != lp_) ? true : false; |
MikamiUitOpen | 0:04b43b777fae | 58 | bool tch = tp_->IsTouched(cursorX_, cursorX_); |
MikamiUitOpen | 0:04b43b777fae | 59 | if (tch || changed) |
MikamiUitOpen | 0:04b43b777fae | 60 | { |
MikamiUitOpen | 0:04b43b777fae | 61 | int newFc = Frq10(drawerObj_->PosToFrq(cursorX_)); |
MikamiUitOpen | 0:04b43b777fae | 62 | newFc = (newFc > HIGHER_F_) ? HIGHER_F_ : newFc; |
MikamiUitOpen | 0:04b43b777fae | 63 | if ((abs(newFc - fC_) >= 10) || changed) |
MikamiUitOpen | 0:04b43b777fae | 64 | { |
MikamiUitOpen | 0:04b43b777fae | 65 | fC_ = newFc; |
MikamiUitOpen | 0:04b43b777fae | 66 | lblFrq_->Draw("Cutoff frequency = %4d Hz", newFc); |
MikamiUitOpen | 0:04b43b777fae | 67 | designObj_->Execute(newFc, lpHp, coefs_, g0_); |
MikamiUitOpen | 0:04b43b777fae | 68 | GetCoefficients(ck, g0); |
MikamiUitOpen | 0:04b43b777fae | 69 | |
MikamiUitOpen | 0:04b43b777fae | 70 | frqResp_.SetParams(ORDER_, g0_, ck_); |
MikamiUitOpen | 0:04b43b777fae | 71 | drawerObj_->Erase(); |
MikamiUitOpen | 0:04b43b777fae | 72 | drawerObj_->DrawAxis(); // 目盛線の描画 |
MikamiUitOpen | 0:04b43b777fae | 73 | drawerObj_->DrawGraph(frqResp_); // 周波数特性のグラフのカーブを描画する |
MikamiUitOpen | 0:04b43b777fae | 74 | |
MikamiUitOpen | 0:04b43b777fae | 75 | if (tch) // カーソルの移動 |
MikamiUitOpen | 0:04b43b777fae | 76 | { |
MikamiUitOpen | 0:04b43b777fae | 77 | lcd_->SetTextColor(CURSOR_TOUCHED_COLOR_); |
MikamiUitOpen | 0:04b43b777fae | 78 | lcd_->DrawVLine(cursorX_, CURSOR_Y0_, CURSOR_LENGTH_); |
MikamiUitOpen | 0:04b43b777fae | 79 | } |
MikamiUitOpen | 0:04b43b777fae | 80 | cursorRedraw_ = true; |
MikamiUitOpen | 0:04b43b777fae | 81 | oldCursorX_ = cursorX_; |
MikamiUitOpen | 0:04b43b777fae | 82 | lp_ = lpHp; |
MikamiUitOpen | 0:04b43b777fae | 83 | return true; |
MikamiUitOpen | 0:04b43b777fae | 84 | } |
MikamiUitOpen | 0:04b43b777fae | 85 | } |
MikamiUitOpen | 0:04b43b777fae | 86 | |
MikamiUitOpen | 0:04b43b777fae | 87 | if (!tch && cursorRedraw_) // カーソルを元の色に戻す |
MikamiUitOpen | 0:04b43b777fae | 88 | { |
MikamiUitOpen | 0:04b43b777fae | 89 | lcd_->SetTextColor(CURSOR_COLOR_); |
MikamiUitOpen | 0:04b43b777fae | 90 | lcd_->DrawVLine(oldCursorX_, CURSOR_Y0_, CURSOR_LENGTH_); |
MikamiUitOpen | 0:04b43b777fae | 91 | cursorRedraw_ = false; |
MikamiUitOpen | 0:04b43b777fae | 92 | } |
MikamiUitOpen | 0:04b43b777fae | 93 | return false; |
MikamiUitOpen | 0:04b43b777fae | 94 | } |
MikamiUitOpen | 0:04b43b777fae | 95 | |
MikamiUitOpen | 0:04b43b777fae | 96 | // 周波数特性の描画 |
MikamiUitOpen | 0:04b43b777fae | 97 | void DesignerDrawer::DrawResponse() |
MikamiUitOpen | 0:04b43b777fae | 98 | { |
MikamiUitOpen | 0:04b43b777fae | 99 | lblFrq_->Draw("Cutoff frequency = %4d Hz", fC_); |
MikamiUitOpen | 0:04b43b777fae | 100 | drawerObj_->DrawAxis(); // 目盛線の描画 |
MikamiUitOpen | 0:04b43b777fae | 101 | FrqRespDrawer::AxisX_Char numX[] = // 横軸の目盛値を描画する際に使う構造体の配列 |
MikamiUitOpen | 0:04b43b777fae | 102 | {{ 100, "0.1"}, { 200, "0.2"}, { 500, "0.5"}, { 1000, "1"}, |
MikamiUitOpen | 0:04b43b777fae | 103 | { 2000, "2"}, { 5000, "5"}, {10000, "10"}, {20000, "20"}}; |
MikamiUitOpen | 0:04b43b777fae | 104 | drawerObj_->DrawNumericX(numX, 8, 6, "Frequency [kHz]"); // 横軸の目盛 |
MikamiUitOpen | 0:04b43b777fae | 105 | drawerObj_->DrawNumericY(-24, -6, 4, 20, "%3d"); // 縦軸の目盛は 20 dB 間隔 |
MikamiUitOpen | 0:04b43b777fae | 106 | drawerObj_->DrawGraph(frqResp_); // 周波数特性のカーブの描画 |
MikamiUitOpen | 0:04b43b777fae | 107 | |
MikamiUitOpen | 0:04b43b777fae | 108 | cursorX_ = drawerObj_->X(fC_); |
MikamiUitOpen | 0:04b43b777fae | 109 | lcd_->SetTextColor(CURSOR_COLOR_); |
MikamiUitOpen | 0:04b43b777fae | 110 | lcd_->DrawVLine(cursorX_, CURSOR_Y0_, CURSOR_LENGTH_); |
MikamiUitOpen | 0:04b43b777fae | 111 | } |
MikamiUitOpen | 0:04b43b777fae | 112 | |
MikamiUitOpen | 0:04b43b777fae | 113 | // フィルタ係数の取得 |
MikamiUitOpen | 0:04b43b777fae | 114 | void DesignerDrawer::GetCoefficients(Biquad::Coefs *c, float &g0) |
MikamiUitOpen | 0:04b43b777fae | 115 | { |
MikamiUitOpen | 0:04b43b777fae | 116 | for (int k=0; k<ORDER_/2; k++) c[k] = ck_[k]; |
MikamiUitOpen | 0:04b43b777fae | 117 | g0 = g0_; |
MikamiUitOpen | 0:04b43b777fae | 118 | } |
MikamiUitOpen | 0:04b43b777fae | 119 | |
MikamiUitOpen | 0:04b43b777fae | 120 | // 周波数を 10, 20, 50, 100 Hz の倍数にする |
MikamiUitOpen | 0:04b43b777fae | 121 | int DesignerDrawer::Frq10(float f) |
MikamiUitOpen | 0:04b43b777fae | 122 | { |
MikamiUitOpen | 0:04b43b777fae | 123 | if (f < 1000) |
MikamiUitOpen | 0:04b43b777fae | 124 | return ((int)(f/10.0f + 0.5f))*10; |
MikamiUitOpen | 0:04b43b777fae | 125 | if (f < 2000) |
MikamiUitOpen | 0:04b43b777fae | 126 | return ((int)(f/20.0f + 0.5f))*20; |
MikamiUitOpen | 0:04b43b777fae | 127 | if (f < 3000) |
MikamiUitOpen | 0:04b43b777fae | 128 | return ((int)(f/50.0f + 0.5f))*50; |
MikamiUitOpen | 0:04b43b777fae | 129 | else |
MikamiUitOpen | 0:04b43b777fae | 130 | return ((int)(f/100.0f + 0.5f))*100; |
MikamiUitOpen | 0:04b43b777fae | 131 | } |
MikamiUitOpen | 0:04b43b777fae | 132 | } |
MikamiUitOpen | 0:04b43b777fae | 133 |