Interface 2015年4月号 第1部 第7章のプログラム
Information
FftTest - Interface 2015年4月号 第1部 第7章 のソフトウェア
Program for Section 7 in April 2015 issue of Interface
(Japanese electronics magazine)
概要
このプログラムは、
- ハイパスフィルタ、ローパスフィルタ、ノッチフィルタ
を行うFilterTestクラス、 - FFT (256点)
を行うFftTestクラス、
波形をUSBシリアル通信でホストへ送信するmain関数で構成されています。
FilterTest.h, FilterTest.cpp
- A-Dサンプリング - 1 kSPS
- ハイパスフィルタ(遮断周波数 0.5 Hz、1次バターワース)
- ローパスフィルタ(遮断周波数 30 Hz、2次バターワース)
- ノッチフィルタ(中心周波数 50 Hz、2次)
FftTest.h, FftTest.cpp
- 256点FFT演算 - クーリー-テューキー アルゴリズム 基数-2 時間間引き
- ハン窓(ハニング窓)適用
- パワー値計算
- 振幅値計算
- 振幅値正規化(実効値にスケーリング)
main.cpp
- データ送信レート - 200 SPS
- メインループ - ポーリングにより、サンプリング、フィルタ処理完了フラグがセットされたら、
また、FFT完了フラグがセットされたらUSBシリアル通信経由で、ホストへ送信する
シリアル通信フォーマット
(※)誌面ではパケットサイズ 64 byteとなっていますが、
64 byteでは、PCのUSBドライバが 4096 byteまで保持し、波形が滑らかに描画できないため、
Ver.1.0.2で、32 byteに変更しています。
- 34byte固定長パケット方式
- 波形データパケット、FFTパケットの2種類
波形データパケット | FFTパケット | |
0x00 | パケットヘッダ(固定値0xAA) | パケットヘッダ(固定値0xAA) |
0x01 | データ種別ID(0x01: 波形データ) | (0x02: FFTデータ) |
0x02 | パケット番号(0 - 99繰り返し) | レンジ(0: DC - 23 Hz, 1: 23 - 46 Hz, 2: 46 - 70 Hz) |
0x03 | ペイロードサイズ(固定値30) | ペイロードサイズ(固定値30) |
0x04 - 0x21 | 波形データ(short, big endian) | FFTデータ(unsigned short, big endian) |
Description
This contains FilterTest class, FftTest class and main function.
FilterTest class:
- High pass filter, Low pass, Notch filter
FftTest class:
- FFT (256 points)
Main function:
- Send waveform and FFT data to host via USB serial class.
FilterTest.h, FilterTest.cpp
- A-D sampling - 1 kSPS
- High pass filter - Cut off frequency 0.5 Hz, first order butterworth
- Low pass filter - Cut off frequency 30 Hz, second order butterworth
- Notch filter - Center frequency 50 Hz, second order
FftTest.h, FftTest.cpp
- 256 points FFT - Cooley-Tukey algorithm Radix-2 Decimation-In-Time
- Apply Hann window
- Calculate power spectrum
- Calculate amplitude spectrum
- Normalize amplitude
main.cpp
- Data sending rate - 200 SPS
- Main loop - sending waveform and FFT data via USB serial interface when detecting ready flag.
Packet format for USB serial interface
- Packet size: 34 bytes(fixed)
- Two types of packet, waveform packet and FFT packet
Waveform packet | FFT packet | |
0x00 | Packet header (0xAA (fixed)) | Packet header (0xAA (fixed)) |
0x01 | Data type ID (0x01: Waveform ID) | (0x02: FFT ID) |
0x02 | Packet number (0 - 99) | Range (0: DC - 23 Hz, 1: 23 - 46 Hz, 2: 46 - 70 Hz) |
0x03 | Payload size (30 (fixed)) | Payload size (30 (fixed)) |
0x04 - 0x21 | Waveform data (short, big endian) | FFT data (unsigned short, big endian) |
Diff: FftTest.h
- Revision:
- 0:9779b89a8820
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/FftTest.h Mon Feb 23 22:19:58 2015 +0000 @@ -0,0 +1,53 @@ +/** + * @file FftTest.h + * @brief Header file for FftTest.cpp + * @date 2015.02.24 + * @version 1.0.2 + */ +#ifndef _INC_FftTest +#define _INC_FftTest + +// #define _LARGE_RAM /* Uncomment if using large RAM. */ +/* Hann window array and bit reversal array can */ +/* be generated dynamically instead of constant. */ + +#include "mbed.h" + +/* Constant */ +#define PI (3.141592) +#define SQRT_2F (1.41421356f) +#define DATA_LENGTH (256) + +/** FFT test + */ +class FftTest +{ + +public: + FftTest(); + ~FftTest(); + void apply_window(float in_data[], float out_data[]); + void calc_fft(float in_data[], float out_re_data[], float out_im_data[]); + void calc_power(float re_data[], float im_data[], float pow_data[], int32_t dat_len); + void calc_amplitude(float pow_data[], float amp_data[], int32_t dat_len); + void norm_amplitude(float amp_data[], int32_t dat_len); + +private: + /* Menber variables */ + int32_t _dat_len; + int32_t _digit_len; +#ifdef _LARGE_RAM + float* _window_array; + int32_t* _br_idx_array; +#else + static const float _window_array[DATA_LENGTH]; /* Hann window array */ + static const int _br_idx_array[DATA_LENGTH]; /* Bit reversal index array */ +#endif /* LARGE_RAM */ + + /* Member functions */ + void init(); + int32_t get_digit_len(int32_t val); + void set_hann_window(); + void set_bit_reversal(); +}; +#endif /* INC_FftTest */