![](/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@12:85e7363da26a, 2019-04-03 (annotated)
- Committer:
- SanYune
- Date:
- Wed Apr 03 04:44:32 2019 +0000
- Revision:
- 12:85e7363da26a
- Parent:
- 5:3e8ca1ed31a1
Chapter006_Zara_F746NG
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 | 5:3e8ca1ed31a1 | 4 | // 2016/05/01, 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 | 5:3e8ca1ed31a1 | 13 | float db1, 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 | 5:3e8ca1ed31a1 | 17 | X0_(x0), Y0_(y0), ORDER_(order), |
MikamiUitOpen | 2:2478e7a8e8d5 | 18 | CURSOR_Y0_(y0+1-db1*60), CURSOR_LENGTH_(db1*60-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 | 2:2478e7a8e8d5 | 23 | y0, -60, 0, db1, 10, 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 | 3:cda40218256c | 32 | frqResp_ = new IIR_CascadeFrqResp(); |
MikamiUitOpen | 3:cda40218256c | 33 | frqResp_->SetParams(ORDER_, g0_, ck_); |
MikamiUitOpen | 0:04b43b777fae | 34 | |
MikamiUitOpen | 0:04b43b777fae | 35 | // 周波数特性の描画 |
MikamiUitOpen | 5:3e8ca1ed31a1 | 36 | lblFrq_ = new NumericLabel<int>(110, 38); |
MikamiUitOpen | 0:04b43b777fae | 37 | DrawResponse(); |
MikamiUitOpen | 0:04b43b777fae | 38 | tp_ = new TouchPanelDetectorX( |
MikamiUitOpen | 0:04b43b777fae | 39 | drawerObj_->X(LOWER_F_), drawerObj_->X(HIGHER_F_), |
MikamiUitOpen | 0:04b43b777fae | 40 | CURSOR_Y0_, y0); |
MikamiUitOpen | 0:04b43b777fae | 41 | lp_ = lpHp; |
MikamiUitOpen | 0:04b43b777fae | 42 | cursorRedraw_ = false; |
MikamiUitOpen | 0:04b43b777fae | 43 | } |
MikamiUitOpen | 0:04b43b777fae | 44 | |
MikamiUitOpen | 0:04b43b777fae | 45 | DesignerDrawer::~DesignerDrawer() |
MikamiUitOpen | 0:04b43b777fae | 46 | { |
MikamiUitOpen | 0:04b43b777fae | 47 | delete tp_; |
MikamiUitOpen | 0:04b43b777fae | 48 | delete lblFrq_; |
MikamiUitOpen | 0:04b43b777fae | 49 | delete coefs_; |
MikamiUitOpen | 0:04b43b777fae | 50 | delete designObj_; |
MikamiUitOpen | 0:04b43b777fae | 51 | delete drawerObj_; |
MikamiUitOpen | 0:04b43b777fae | 52 | } |
MikamiUitOpen | 0:04b43b777fae | 53 | |
MikamiUitOpen | 0:04b43b777fae | 54 | // フィルタの再設計と周波数特性の再描画 |
MikamiUitOpen | 0:04b43b777fae | 55 | bool DesignerDrawer::ReDesignAndDraw(Biquad::Coefs *ck, float &g0, |
MikamiUitOpen | 0:04b43b777fae | 56 | BilinearDesign::Type lpHp) |
MikamiUitOpen | 0:04b43b777fae | 57 | { |
MikamiUitOpen | 0:04b43b777fae | 58 | bool changed = (lpHp != lp_) ? true : false; |
MikamiUitOpen | 0:04b43b777fae | 59 | bool tch = tp_->IsTouched(cursorX_, cursorX_); |
MikamiUitOpen | 0:04b43b777fae | 60 | if (tch || changed) |
MikamiUitOpen | 0:04b43b777fae | 61 | { |
MikamiUitOpen | 0:04b43b777fae | 62 | int newFc = Frq10(drawerObj_->PosToFrq(cursorX_)); |
MikamiUitOpen | 0:04b43b777fae | 63 | newFc = (newFc > HIGHER_F_) ? HIGHER_F_ : newFc; |
MikamiUitOpen | 0:04b43b777fae | 64 | if ((abs(newFc - fC_) >= 10) || changed) |
MikamiUitOpen | 0:04b43b777fae | 65 | { |
MikamiUitOpen | 0:04b43b777fae | 66 | fC_ = newFc; |
MikamiUitOpen | 0:04b43b777fae | 67 | lblFrq_->Draw("Cutoff frequency = %4d Hz", newFc); |
MikamiUitOpen | 0:04b43b777fae | 68 | designObj_->Execute(newFc, lpHp, coefs_, g0_); |
MikamiUitOpen | 0:04b43b777fae | 69 | GetCoefficients(ck, g0); |
MikamiUitOpen | 0:04b43b777fae | 70 | |
MikamiUitOpen | 3:cda40218256c | 71 | frqResp_->SetParams(ORDER_, g0_, ck_); |
MikamiUitOpen | 0:04b43b777fae | 72 | drawerObj_->Erase(); |
MikamiUitOpen | 0:04b43b777fae | 73 | drawerObj_->DrawAxis(); // 目盛線の描画 |
MikamiUitOpen | 0:04b43b777fae | 74 | drawerObj_->DrawGraph(frqResp_); // 周波数特性のグラフのカーブを描画する |
MikamiUitOpen | 0:04b43b777fae | 75 | |
MikamiUitOpen | 0:04b43b777fae | 76 | if (tch) // カーソルの移動 |
MikamiUitOpen | 0:04b43b777fae | 77 | { |
MikamiUitOpen | 0:04b43b777fae | 78 | lcd_->SetTextColor(CURSOR_TOUCHED_COLOR_); |
MikamiUitOpen | 0:04b43b777fae | 79 | lcd_->DrawVLine(cursorX_, CURSOR_Y0_, CURSOR_LENGTH_); |
MikamiUitOpen | 0:04b43b777fae | 80 | } |
MikamiUitOpen | 0:04b43b777fae | 81 | cursorRedraw_ = true; |
MikamiUitOpen | 0:04b43b777fae | 82 | oldCursorX_ = cursorX_; |
MikamiUitOpen | 0:04b43b777fae | 83 | lp_ = lpHp; |
MikamiUitOpen | 0:04b43b777fae | 84 | return true; |
MikamiUitOpen | 0:04b43b777fae | 85 | } |
MikamiUitOpen | 0:04b43b777fae | 86 | } |
MikamiUitOpen | 0:04b43b777fae | 87 | |
MikamiUitOpen | 0:04b43b777fae | 88 | if (!tch && cursorRedraw_) // カーソルを元の色に戻す |
MikamiUitOpen | 0:04b43b777fae | 89 | { |
MikamiUitOpen | 0:04b43b777fae | 90 | lcd_->SetTextColor(CURSOR_COLOR_); |
MikamiUitOpen | 0:04b43b777fae | 91 | lcd_->DrawVLine(oldCursorX_, CURSOR_Y0_, CURSOR_LENGTH_); |
MikamiUitOpen | 0:04b43b777fae | 92 | cursorRedraw_ = false; |
MikamiUitOpen | 0:04b43b777fae | 93 | } |
MikamiUitOpen | 0:04b43b777fae | 94 | return false; |
MikamiUitOpen | 0:04b43b777fae | 95 | } |
MikamiUitOpen | 0:04b43b777fae | 96 | |
MikamiUitOpen | 0:04b43b777fae | 97 | // 周波数特性の描画 |
MikamiUitOpen | 0:04b43b777fae | 98 | void DesignerDrawer::DrawResponse() |
MikamiUitOpen | 0:04b43b777fae | 99 | { |
MikamiUitOpen | 0:04b43b777fae | 100 | lblFrq_->Draw("Cutoff frequency = %4d Hz", fC_); |
MikamiUitOpen | 0:04b43b777fae | 101 | drawerObj_->DrawAxis(); // 目盛線の描画 |
MikamiUitOpen | 0:04b43b777fae | 102 | FrqRespDrawer::AxisX_Char numX[] = // 横軸の目盛値を描画する際に使う構造体の配列 |
MikamiUitOpen | 0:04b43b777fae | 103 | {{ 100, "0.1"}, { 200, "0.2"}, { 500, "0.5"}, { 1000, "1"}, |
MikamiUitOpen | 0:04b43b777fae | 104 | { 2000, "2"}, { 5000, "5"}, {10000, "10"}, {20000, "20"}}; |
MikamiUitOpen | 2:2478e7a8e8d5 | 105 | drawerObj_->DrawNumericX(numX, 8, 6, "Frequency [kHz]"); // 横軸の目盛 |
MikamiUitOpen | 2:2478e7a8e8d5 | 106 | drawerObj_->DrawNumericY(-24, -6, 20, "%3d"); // 縦軸の目盛は 20 dB 間隔 |
MikamiUitOpen | 0:04b43b777fae | 107 | drawerObj_->DrawGraph(frqResp_); // 周波数特性のカーブの描画 |
MikamiUitOpen | 0:04b43b777fae | 108 | |
MikamiUitOpen | 0:04b43b777fae | 109 | cursorX_ = drawerObj_->X(fC_); |
MikamiUitOpen | 0:04b43b777fae | 110 | lcd_->SetTextColor(CURSOR_COLOR_); |
MikamiUitOpen | 0:04b43b777fae | 111 | lcd_->DrawVLine(cursorX_, CURSOR_Y0_, CURSOR_LENGTH_); |
MikamiUitOpen | 0:04b43b777fae | 112 | } |
MikamiUitOpen | 0:04b43b777fae | 113 | |
MikamiUitOpen | 0:04b43b777fae | 114 | // フィルタ係数の取得 |
MikamiUitOpen | 0:04b43b777fae | 115 | void DesignerDrawer::GetCoefficients(Biquad::Coefs *c, float &g0) |
MikamiUitOpen | 0:04b43b777fae | 116 | { |
MikamiUitOpen | 0:04b43b777fae | 117 | for (int k=0; k<ORDER_/2; k++) c[k] = ck_[k]; |
MikamiUitOpen | 0:04b43b777fae | 118 | g0 = g0_; |
MikamiUitOpen | 0:04b43b777fae | 119 | } |
MikamiUitOpen | 0:04b43b777fae | 120 | |
MikamiUitOpen | 0:04b43b777fae | 121 | // 周波数を 10, 20, 50, 100 Hz の倍数にする |
MikamiUitOpen | 0:04b43b777fae | 122 | int DesignerDrawer::Frq10(float f) |
MikamiUitOpen | 0:04b43b777fae | 123 | { |
MikamiUitOpen | 0:04b43b777fae | 124 | if (f < 1000) |
MikamiUitOpen | 0:04b43b777fae | 125 | return ((int)(f/10.0f + 0.5f))*10; |
MikamiUitOpen | 0:04b43b777fae | 126 | if (f < 2000) |
MikamiUitOpen | 0:04b43b777fae | 127 | return ((int)(f/20.0f + 0.5f))*20; |
MikamiUitOpen | 0:04b43b777fae | 128 | if (f < 3000) |
MikamiUitOpen | 0:04b43b777fae | 129 | return ((int)(f/50.0f + 0.5f))*50; |
MikamiUitOpen | 0:04b43b777fae | 130 | else |
MikamiUitOpen | 0:04b43b777fae | 131 | return ((int)(f/100.0f + 0.5f))*100; |
MikamiUitOpen | 0:04b43b777fae | 132 | } |
MikamiUitOpen | 0:04b43b777fae | 133 | } |
MikamiUitOpen | 5:3e8ca1ed31a1 | 134 |