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) |
main.cpp@0:9779b89a8820, 2015-02-23 (annotated)
- Committer:
- t_tatsuoka
- Date:
- Mon Feb 23 22:19:58 2015 +0000
- Revision:
- 0:9779b89a8820
Ver. 1.0.2 Initial version
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
t_tatsuoka | 0:9779b89a8820 | 1 | /** |
t_tatsuoka | 0:9779b89a8820 | 2 | * @file Main.cpp |
t_tatsuoka | 0:9779b89a8820 | 3 | * @brief Send 1ch waveform and FFT data to PC via USB serial |
t_tatsuoka | 0:9779b89a8820 | 4 | * @date 2015.02.24 |
t_tatsuoka | 0:9779b89a8820 | 5 | * @version 1.0.2 |
t_tatsuoka | 0:9779b89a8820 | 6 | */ |
t_tatsuoka | 0:9779b89a8820 | 7 | #include "mbed.h" |
t_tatsuoka | 0:9779b89a8820 | 8 | #include "USBSerial.h" |
t_tatsuoka | 0:9779b89a8820 | 9 | #include "FilterTest.h" |
t_tatsuoka | 0:9779b89a8820 | 10 | #include "FftTest.h" |
t_tatsuoka | 0:9779b89a8820 | 11 | |
t_tatsuoka | 0:9779b89a8820 | 12 | #define ON (1) |
t_tatsuoka | 0:9779b89a8820 | 13 | #define OFF (0) |
t_tatsuoka | 0:9779b89a8820 | 14 | |
t_tatsuoka | 0:9779b89a8820 | 15 | #define LED_ON (0) |
t_tatsuoka | 0:9779b89a8820 | 16 | #define LED_OFF (1) |
t_tatsuoka | 0:9779b89a8820 | 17 | |
t_tatsuoka | 0:9779b89a8820 | 18 | #define BYTE_MASK (0xFF) |
t_tatsuoka | 0:9779b89a8820 | 19 | #define INT16_MAX (32767) |
t_tatsuoka | 0:9779b89a8820 | 20 | #define INT16_MIN (-32768) |
t_tatsuoka | 0:9779b89a8820 | 21 | #define UINT16_MAX (65535) |
t_tatsuoka | 0:9779b89a8820 | 22 | #define UINT16_MIN (0) |
t_tatsuoka | 0:9779b89a8820 | 23 | |
t_tatsuoka | 0:9779b89a8820 | 24 | #define SAMPLING_RATE (0.001) /* A/D sampling rate (1kHz) */ |
t_tatsuoka | 0:9779b89a8820 | 25 | #define DEC_COUNT (5) /* Decimation count (sampling 200Hz)*/ |
t_tatsuoka | 0:9779b89a8820 | 26 | |
t_tatsuoka | 0:9779b89a8820 | 27 | #define PACKET_HEADER (0xAA) |
t_tatsuoka | 0:9779b89a8820 | 28 | #define WAVEFORM_ID (0x01) |
t_tatsuoka | 0:9779b89a8820 | 29 | #define FFT_ID (0x02) |
t_tatsuoka | 0:9779b89a8820 | 30 | #define PACKET_SIZE (34) /* 64 -> 34 for PC to receive waveform data smoothly */ |
t_tatsuoka | 0:9779b89a8820 | 31 | #define PAYLOAD_SIZE (30) /* PACKET_SIZE - 4(Header size) */ |
t_tatsuoka | 0:9779b89a8820 | 32 | #define WAV_BUF_NUM (2) /* Double buffer */ |
t_tatsuoka | 0:9779b89a8820 | 33 | #define WV_PKT_CNTR_NUM (100) |
t_tatsuoka | 0:9779b89a8820 | 34 | /* [ Packet format ] */ |
t_tatsuoka | 0:9779b89a8820 | 35 | /* ---------------- */ |
t_tatsuoka | 0:9779b89a8820 | 36 | /* 0x00 | Header byte | Fixed value (PACKET_HEADER:0xAA) */ |
t_tatsuoka | 0:9779b89a8820 | 37 | /* 0x01 | Data type | ID value (0x01:Waveform, 0x02:FFT) */ |
t_tatsuoka | 0:9779b89a8820 | 38 | /* 0x02 | Packet counter | Count value (0to99 < CNTR_SIZE:100) */ |
t_tatsuoka | 0:9779b89a8820 | 39 | /* 0x03 | Payload size | Fixed value (DATA_NUM:30) */ |
t_tatsuoka | 0:9779b89a8820 | 40 | /* 0x04 | Data0 (MSBside)| Short type (signed, big endian) */ |
t_tatsuoka | 0:9779b89a8820 | 41 | /* ... | ... | */ |
t_tatsuoka | 0:9779b89a8820 | 42 | /* 0x21 | Data29(LSBside)| */ |
t_tatsuoka | 0:9779b89a8820 | 43 | /* ---------------- */ |
t_tatsuoka | 0:9779b89a8820 | 44 | |
t_tatsuoka | 0:9779b89a8820 | 45 | #define FFT_LENGTH (256) /* FFT data length */ |
t_tatsuoka | 0:9779b89a8820 | 46 | #define FFT_INTERVAL (200) /* FFT calc interval */ |
t_tatsuoka | 0:9779b89a8820 | 47 | #define FFT_OVERLAP_LEN (56) /* FFT overlap length */ |
t_tatsuoka | 0:9779b89a8820 | 48 | #define FFT_BUF_NUM (2) /* Double buffer */ |
t_tatsuoka | 0:9779b89a8820 | 49 | #define FFT_SEND_LEN (90) /* FFT send data length */ |
t_tatsuoka | 0:9779b89a8820 | 50 | |
t_tatsuoka | 0:9779b89a8820 | 51 | |
t_tatsuoka | 0:9779b89a8820 | 52 | Ticker sampling; /* Interval timer for A/D sampling */ |
t_tatsuoka | 0:9779b89a8820 | 53 | AnalogIn wave_in(p20); /* Waveform input */ |
t_tatsuoka | 0:9779b89a8820 | 54 | USBSerial serial; |
t_tatsuoka | 0:9779b89a8820 | 55 | FilterTest filter; |
t_tatsuoka | 0:9779b89a8820 | 56 | FftTest fft; |
t_tatsuoka | 0:9779b89a8820 | 57 | |
t_tatsuoka | 0:9779b89a8820 | 58 | /* DIP switch for filter ON/OFF */ |
t_tatsuoka | 0:9779b89a8820 | 59 | DigitalIn hpf_on(p28); |
t_tatsuoka | 0:9779b89a8820 | 60 | DigitalIn lpf_on(p27); |
t_tatsuoka | 0:9779b89a8820 | 61 | DigitalIn brf_on(p26); |
t_tatsuoka | 0:9779b89a8820 | 62 | /* [ DIP switch ] */ |
t_tatsuoka | 0:9779b89a8820 | 63 | /* TG-LPC11U35-501 +3.3V */ |
t_tatsuoka | 0:9779b89a8820 | 64 | /* | CN1 CN2 | --- */ |
t_tatsuoka | 0:9779b89a8820 | 65 | /* | mbed BD | | */ |
t_tatsuoka | 0:9779b89a8820 | 66 | /* | 13 | p28 -/ -+ Hiph pass filter */ |
t_tatsuoka | 0:9779b89a8820 | 67 | /* | 14 | p27 -/ -+ Low pass filter */ |
t_tatsuoka | 0:9779b89a8820 | 68 | /* | 15 | p26 -/ -+ Notch filter */ |
t_tatsuoka | 0:9779b89a8820 | 69 | /* | | */ |
t_tatsuoka | 0:9779b89a8820 | 70 | /* pull-down is default as below. */ |
t_tatsuoka | 0:9779b89a8820 | 71 | /* http://developer.mbed.org/handbook/DigitalIn */ |
t_tatsuoka | 0:9779b89a8820 | 72 | /* It says "By default, the DigitalIn is setup */ |
t_tatsuoka | 0:9779b89a8820 | 73 | /* with an internal pull-down resistor." */ |
t_tatsuoka | 0:9779b89a8820 | 74 | /* Better to set the pins to pull-up mode and the */ |
t_tatsuoka | 0:9779b89a8820 | 75 | /* switch connected to GND. */ |
t_tatsuoka | 0:9779b89a8820 | 76 | |
t_tatsuoka | 0:9779b89a8820 | 77 | /* For debug pins */ |
t_tatsuoka | 0:9779b89a8820 | 78 | DigitalOut dbg_p21(p21); |
t_tatsuoka | 0:9779b89a8820 | 79 | DigitalOut dbg_p22(p22); |
t_tatsuoka | 0:9779b89a8820 | 80 | DigitalOut dbg_led1(LED1); |
t_tatsuoka | 0:9779b89a8820 | 81 | DigitalOut dbg_led2(LED2); |
t_tatsuoka | 0:9779b89a8820 | 82 | |
t_tatsuoka | 0:9779b89a8820 | 83 | /* Variables */ |
t_tatsuoka | 0:9779b89a8820 | 84 | int32_t wav_dec_cntr; /* Decimation counter */ |
t_tatsuoka | 0:9779b89a8820 | 85 | |
t_tatsuoka | 0:9779b89a8820 | 86 | uint8_t wav_pkt_buf[WAV_BUF_NUM][PACKET_SIZE]; /* Waveform packet buffer */ |
t_tatsuoka | 0:9779b89a8820 | 87 | int32_t wav_pkt_idx; /* Packet buffer index */ |
t_tatsuoka | 0:9779b89a8820 | 88 | int32_t wav_tgl; /* Packet buffer toggle */ |
t_tatsuoka | 0:9779b89a8820 | 89 | int32_t wav_send_flag; /* Waveform sending flag */ |
t_tatsuoka | 0:9779b89a8820 | 90 | uint8_t wav_pkt_cntr; /* Waveform packet counter */ |
t_tatsuoka | 0:9779b89a8820 | 91 | |
t_tatsuoka | 0:9779b89a8820 | 92 | float fft_buf[FFT_BUF_NUM][FFT_LENGTH]; /* FFT data buffer */ |
t_tatsuoka | 0:9779b89a8820 | 93 | uint8_t pkt_buf_fft[PACKET_SIZE] = {0}; /* FFT packet buffer */ |
t_tatsuoka | 0:9779b89a8820 | 94 | int32_t fft_dat_idx; /* FFT buffer index */ |
t_tatsuoka | 0:9779b89a8820 | 95 | int32_t fft_tgl; /* FFT buffer toggle */ |
t_tatsuoka | 0:9779b89a8820 | 96 | int32_t fft_send_flag; /* FFT sendign flag */ |
t_tatsuoka | 0:9779b89a8820 | 97 | |
t_tatsuoka | 0:9779b89a8820 | 98 | float fft_out_re[FFT_LENGTH] = {0.0f}; /* FFT result real */ |
t_tatsuoka | 0:9779b89a8820 | 99 | float fft_out_im[FFT_LENGTH] = {0.0f}; /* FFT result imaginary */ |
t_tatsuoka | 0:9779b89a8820 | 100 | uint16_t fft_res[FFT_SEND_LEN] = {0}; /* FFT result */ |
t_tatsuoka | 0:9779b89a8820 | 101 | |
t_tatsuoka | 0:9779b89a8820 | 102 | /** Initialize waveform packet buffer |
t_tatsuoka | 0:9779b89a8820 | 103 | * @param tgl buffer wav_tgl index |
t_tatsuoka | 0:9779b89a8820 | 104 | * @param ctr packet counter |
t_tatsuoka | 0:9779b89a8820 | 105 | */ |
t_tatsuoka | 0:9779b89a8820 | 106 | void init_wav_pkt_buf(int32_t tgl, uint8_t ctr) |
t_tatsuoka | 0:9779b89a8820 | 107 | { |
t_tatsuoka | 0:9779b89a8820 | 108 | int i; |
t_tatsuoka | 0:9779b89a8820 | 109 | if(tgl>WAV_BUF_NUM) { |
t_tatsuoka | 0:9779b89a8820 | 110 | return; |
t_tatsuoka | 0:9779b89a8820 | 111 | } |
t_tatsuoka | 0:9779b89a8820 | 112 | wav_pkt_buf[tgl][0] = PACKET_HEADER; |
t_tatsuoka | 0:9779b89a8820 | 113 | wav_pkt_buf[tgl][1] = WAVEFORM_ID; |
t_tatsuoka | 0:9779b89a8820 | 114 | wav_pkt_buf[tgl][2] = ctr; /* Packet counter */ |
t_tatsuoka | 0:9779b89a8820 | 115 | wav_pkt_buf[tgl][3] = PAYLOAD_SIZE; /* Payload size */ |
t_tatsuoka | 0:9779b89a8820 | 116 | wav_pkt_idx = 4; /* Start index of waveform */ |
t_tatsuoka | 0:9779b89a8820 | 117 | for(i=4; i<PACKET_SIZE; i++) { |
t_tatsuoka | 0:9779b89a8820 | 118 | wav_pkt_buf[tgl][i] = 0; |
t_tatsuoka | 0:9779b89a8820 | 119 | } |
t_tatsuoka | 0:9779b89a8820 | 120 | } |
t_tatsuoka | 0:9779b89a8820 | 121 | |
t_tatsuoka | 0:9779b89a8820 | 122 | /** Set FFT packet buffer |
t_tatsuoka | 0:9779b89a8820 | 123 | * @param buf Source data buffer |
t_tatsuoka | 0:9779b89a8820 | 124 | * @param ctr packet counter |
t_tatsuoka | 0:9779b89a8820 | 125 | */ |
t_tatsuoka | 0:9779b89a8820 | 126 | void set_fft_pkt_buf(uint16_t* buf, uint8_t ctr) |
t_tatsuoka | 0:9779b89a8820 | 127 | { |
t_tatsuoka | 0:9779b89a8820 | 128 | int i; |
t_tatsuoka | 0:9779b89a8820 | 129 | pkt_buf_fft[0] = PACKET_HEADER; |
t_tatsuoka | 0:9779b89a8820 | 130 | pkt_buf_fft[1] = FFT_ID; |
t_tatsuoka | 0:9779b89a8820 | 131 | pkt_buf_fft[2] = ctr; /* Packet counter */ |
t_tatsuoka | 0:9779b89a8820 | 132 | pkt_buf_fft[3] = PAYLOAD_SIZE; /* Payload size */ |
t_tatsuoka | 0:9779b89a8820 | 133 | i=0; |
t_tatsuoka | 0:9779b89a8820 | 134 | while(i<PAYLOAD_SIZE) { |
t_tatsuoka | 0:9779b89a8820 | 135 | pkt_buf_fft[i + 4] = (uint8_t)((buf[i / 2] >> 8 ) & BYTE_MASK); |
t_tatsuoka | 0:9779b89a8820 | 136 | i++; |
t_tatsuoka | 0:9779b89a8820 | 137 | pkt_buf_fft[i + 4] = (uint8_t)(buf[i / 2] & BYTE_MASK); |
t_tatsuoka | 0:9779b89a8820 | 138 | i++; |
t_tatsuoka | 0:9779b89a8820 | 139 | } |
t_tatsuoka | 0:9779b89a8820 | 140 | } |
t_tatsuoka | 0:9779b89a8820 | 141 | |
t_tatsuoka | 0:9779b89a8820 | 142 | /** Interval timer for read A/D value |
t_tatsuoka | 0:9779b89a8820 | 143 | */ |
t_tatsuoka | 0:9779b89a8820 | 144 | void ad_sampling() |
t_tatsuoka | 0:9779b89a8820 | 145 | { |
t_tatsuoka | 0:9779b89a8820 | 146 | int32_t wav_temp; |
t_tatsuoka | 0:9779b89a8820 | 147 | dbg_led2 = lpf_on; /* for debug */ |
t_tatsuoka | 0:9779b89a8820 | 148 | |
t_tatsuoka | 0:9779b89a8820 | 149 | /* Read and filter data */ |
t_tatsuoka | 0:9779b89a8820 | 150 | wav_temp = (int32_t)filter.calc( (double)(wave_in.read_u16() - INT16_MAX), hpf_on, lpf_on, brf_on ); |
t_tatsuoka | 0:9779b89a8820 | 151 | |
t_tatsuoka | 0:9779b89a8820 | 152 | /* Store data */ |
t_tatsuoka | 0:9779b89a8820 | 153 | wav_dec_cntr = (wav_dec_cntr + 1) % DEC_COUNT; |
t_tatsuoka | 0:9779b89a8820 | 154 | if(wav_dec_cntr == 0) { /* Decimation */ |
t_tatsuoka | 0:9779b89a8820 | 155 | |
t_tatsuoka | 0:9779b89a8820 | 156 | dbg_p21 = ON; /* for debug */ |
t_tatsuoka | 0:9779b89a8820 | 157 | /* Waveform */ |
t_tatsuoka | 0:9779b89a8820 | 158 | wav_temp = (wav_temp > INT16_MAX) ? INT16_MAX : wav_temp; |
t_tatsuoka | 0:9779b89a8820 | 159 | wav_temp = (wav_temp < INT16_MIN) ? INT16_MIN : wav_temp; |
t_tatsuoka | 0:9779b89a8820 | 160 | wav_pkt_buf[wav_tgl][wav_pkt_idx] = (uint8_t)((wav_temp >> 8 ) & BYTE_MASK); |
t_tatsuoka | 0:9779b89a8820 | 161 | wav_pkt_idx++; |
t_tatsuoka | 0:9779b89a8820 | 162 | wav_pkt_buf[wav_tgl][wav_pkt_idx] = (uint8_t)(wav_temp & BYTE_MASK); |
t_tatsuoka | 0:9779b89a8820 | 163 | wav_pkt_idx++; |
t_tatsuoka | 0:9779b89a8820 | 164 | |
t_tatsuoka | 0:9779b89a8820 | 165 | if(wav_pkt_idx >= PACKET_SIZE) { /* Counter reached */ |
t_tatsuoka | 0:9779b89a8820 | 166 | wav_tgl = !wav_tgl; |
t_tatsuoka | 0:9779b89a8820 | 167 | wav_pkt_cntr = (wav_pkt_cntr + 1 ) % WV_PKT_CNTR_NUM; |
t_tatsuoka | 0:9779b89a8820 | 168 | init_wav_pkt_buf(wav_tgl, wav_pkt_cntr); |
t_tatsuoka | 0:9779b89a8820 | 169 | wav_send_flag = ON; /* Set flag */ |
t_tatsuoka | 0:9779b89a8820 | 170 | } |
t_tatsuoka | 0:9779b89a8820 | 171 | |
t_tatsuoka | 0:9779b89a8820 | 172 | /* FFT */ |
t_tatsuoka | 0:9779b89a8820 | 173 | fft_buf[fft_tgl][fft_dat_idx + FFT_OVERLAP_LEN] = (float)wav_temp; |
t_tatsuoka | 0:9779b89a8820 | 174 | if(fft_dat_idx >= (FFT_INTERVAL - FFT_OVERLAP_LEN)) { |
t_tatsuoka | 0:9779b89a8820 | 175 | fft_buf[!fft_tgl][fft_dat_idx + FFT_OVERLAP_LEN - FFT_INTERVAL] = (float)wav_temp; |
t_tatsuoka | 0:9779b89a8820 | 176 | } |
t_tatsuoka | 0:9779b89a8820 | 177 | |
t_tatsuoka | 0:9779b89a8820 | 178 | fft_dat_idx = (fft_dat_idx + 1) % FFT_INTERVAL; |
t_tatsuoka | 0:9779b89a8820 | 179 | if(fft_dat_idx == 0) { |
t_tatsuoka | 0:9779b89a8820 | 180 | fft_tgl = !fft_tgl; |
t_tatsuoka | 0:9779b89a8820 | 181 | fft_send_flag = ON; /* Set flag */ |
t_tatsuoka | 0:9779b89a8820 | 182 | } |
t_tatsuoka | 0:9779b89a8820 | 183 | |
t_tatsuoka | 0:9779b89a8820 | 184 | dbg_p21 = OFF; /* for debug */ |
t_tatsuoka | 0:9779b89a8820 | 185 | } |
t_tatsuoka | 0:9779b89a8820 | 186 | } |
t_tatsuoka | 0:9779b89a8820 | 187 | |
t_tatsuoka | 0:9779b89a8820 | 188 | /** Send data packet |
t_tatsuoka | 0:9779b89a8820 | 189 | * @param p_buf Data array |
t_tatsuoka | 0:9779b89a8820 | 190 | * @param size Data length |
t_tatsuoka | 0:9779b89a8820 | 191 | */ |
t_tatsuoka | 0:9779b89a8820 | 192 | bool send_packet(uint8_t *p_buf, uint16_t size) |
t_tatsuoka | 0:9779b89a8820 | 193 | { |
t_tatsuoka | 0:9779b89a8820 | 194 | if(serial.writeable()) { |
t_tatsuoka | 0:9779b89a8820 | 195 | dbg_p22 = ON; /* for debug */ |
t_tatsuoka | 0:9779b89a8820 | 196 | serial.writeBlock (p_buf, size); /* Send data via USB */ |
t_tatsuoka | 0:9779b89a8820 | 197 | dbg_p22 = OFF; /* for debug */ |
t_tatsuoka | 0:9779b89a8820 | 198 | return true; |
t_tatsuoka | 0:9779b89a8820 | 199 | } else { |
t_tatsuoka | 0:9779b89a8820 | 200 | return false; |
t_tatsuoka | 0:9779b89a8820 | 201 | } |
t_tatsuoka | 0:9779b89a8820 | 202 | } |
t_tatsuoka | 0:9779b89a8820 | 203 | |
t_tatsuoka | 0:9779b89a8820 | 204 | /** Main function |
t_tatsuoka | 0:9779b89a8820 | 205 | */ |
t_tatsuoka | 0:9779b89a8820 | 206 | int main() |
t_tatsuoka | 0:9779b89a8820 | 207 | { |
t_tatsuoka | 0:9779b89a8820 | 208 | int i; |
t_tatsuoka | 0:9779b89a8820 | 209 | uint16_t fft_temp; |
t_tatsuoka | 0:9779b89a8820 | 210 | |
t_tatsuoka | 0:9779b89a8820 | 211 | /* Initialization */ |
t_tatsuoka | 0:9779b89a8820 | 212 | wav_dec_cntr = 0; |
t_tatsuoka | 0:9779b89a8820 | 213 | |
t_tatsuoka | 0:9779b89a8820 | 214 | init_wav_pkt_buf(wav_tgl, wav_pkt_cntr); |
t_tatsuoka | 0:9779b89a8820 | 215 | init_wav_pkt_buf(!wav_tgl, wav_pkt_cntr); |
t_tatsuoka | 0:9779b89a8820 | 216 | wav_pkt_idx = 0; |
t_tatsuoka | 0:9779b89a8820 | 217 | wav_tgl = 0; |
t_tatsuoka | 0:9779b89a8820 | 218 | wav_send_flag = OFF; |
t_tatsuoka | 0:9779b89a8820 | 219 | wav_pkt_cntr = 0; |
t_tatsuoka | 0:9779b89a8820 | 220 | |
t_tatsuoka | 0:9779b89a8820 | 221 | fft_dat_idx = 0; |
t_tatsuoka | 0:9779b89a8820 | 222 | fft_tgl = 0; |
t_tatsuoka | 0:9779b89a8820 | 223 | fft_send_flag = OFF; |
t_tatsuoka | 0:9779b89a8820 | 224 | |
t_tatsuoka | 0:9779b89a8820 | 225 | dbg_p21 = OFF; |
t_tatsuoka | 0:9779b89a8820 | 226 | dbg_p22 = OFF; |
t_tatsuoka | 0:9779b89a8820 | 227 | dbg_led1 = LED_OFF; |
t_tatsuoka | 0:9779b89a8820 | 228 | dbg_led2 = LED_OFF; |
t_tatsuoka | 0:9779b89a8820 | 229 | |
t_tatsuoka | 0:9779b89a8820 | 230 | /* Start interval timer */ |
t_tatsuoka | 0:9779b89a8820 | 231 | sampling.attach(&ad_sampling, SAMPLING_RATE); |
t_tatsuoka | 0:9779b89a8820 | 232 | |
t_tatsuoka | 0:9779b89a8820 | 233 | /* Main loop */ |
t_tatsuoka | 0:9779b89a8820 | 234 | while(1) { |
t_tatsuoka | 0:9779b89a8820 | 235 | /* Waveform */ |
t_tatsuoka | 0:9779b89a8820 | 236 | if(wav_send_flag != OFF) { |
t_tatsuoka | 0:9779b89a8820 | 237 | /* Send data */ |
t_tatsuoka | 0:9779b89a8820 | 238 | send_packet(wav_pkt_buf[!wav_tgl], (uint16_t)PACKET_SIZE); |
t_tatsuoka | 0:9779b89a8820 | 239 | |
t_tatsuoka | 0:9779b89a8820 | 240 | /* Disable interrupt */ |
t_tatsuoka | 0:9779b89a8820 | 241 | __disable_irq(); |
t_tatsuoka | 0:9779b89a8820 | 242 | wav_send_flag = OFF; /* Clear flag */ |
t_tatsuoka | 0:9779b89a8820 | 243 | __enable_irq(); |
t_tatsuoka | 0:9779b89a8820 | 244 | } |
t_tatsuoka | 0:9779b89a8820 | 245 | |
t_tatsuoka | 0:9779b89a8820 | 246 | /* FFT */ |
t_tatsuoka | 0:9779b89a8820 | 247 | if(fft_send_flag != OFF) { |
t_tatsuoka | 0:9779b89a8820 | 248 | dbg_led1 = LED_ON; /* for debug */ |
t_tatsuoka | 0:9779b89a8820 | 249 | |
t_tatsuoka | 0:9779b89a8820 | 250 | /* Calculate FFT and normalized amplitude spectrum */ |
t_tatsuoka | 0:9779b89a8820 | 251 | fft.apply_window(fft_buf[!fft_tgl], fft_buf[!fft_tgl]); |
t_tatsuoka | 0:9779b89a8820 | 252 | fft.calc_fft(fft_buf[!fft_tgl], fft_out_re, fft_out_im); |
t_tatsuoka | 0:9779b89a8820 | 253 | fft.calc_power(fft_out_re, fft_out_im, fft_out_re, FFT_SEND_LEN); |
t_tatsuoka | 0:9779b89a8820 | 254 | fft.calc_amplitude(fft_out_re, fft_out_re, FFT_SEND_LEN); |
t_tatsuoka | 0:9779b89a8820 | 255 | fft.norm_amplitude(fft_out_re, FFT_SEND_LEN); |
t_tatsuoka | 0:9779b89a8820 | 256 | for(i=0; i<FFT_SEND_LEN; i++) { |
t_tatsuoka | 0:9779b89a8820 | 257 | fft_temp = (fft_out_re[i] > UINT16_MAX) ? UINT16_MAX : (uint16_t)fft_out_re[i]; |
t_tatsuoka | 0:9779b89a8820 | 258 | fft_temp = (fft_out_re[i] < UINT16_MIN) ? UINT16_MIN : fft_temp; |
t_tatsuoka | 0:9779b89a8820 | 259 | fft_res[i] = fft_temp; |
t_tatsuoka | 0:9779b89a8820 | 260 | } |
t_tatsuoka | 0:9779b89a8820 | 261 | |
t_tatsuoka | 0:9779b89a8820 | 262 | /* Send data */ |
t_tatsuoka | 0:9779b89a8820 | 263 | i=0; |
t_tatsuoka | 0:9779b89a8820 | 264 | while((PAYLOAD_SIZE * i) < FFT_SEND_LEN * sizeof(fft_res[0])) { |
t_tatsuoka | 0:9779b89a8820 | 265 | set_fft_pkt_buf((fft_res + (PAYLOAD_SIZE/sizeof(fft_res[0])) * i), i); |
t_tatsuoka | 0:9779b89a8820 | 266 | send_packet(pkt_buf_fft, (uint16_t)PACKET_SIZE); |
t_tatsuoka | 0:9779b89a8820 | 267 | i++; |
t_tatsuoka | 0:9779b89a8820 | 268 | } |
t_tatsuoka | 0:9779b89a8820 | 269 | |
t_tatsuoka | 0:9779b89a8820 | 270 | /* Disable interrupt */ |
t_tatsuoka | 0:9779b89a8820 | 271 | __disable_irq(); |
t_tatsuoka | 0:9779b89a8820 | 272 | fft_send_flag = OFF; /* Clear flag */ |
t_tatsuoka | 0:9779b89a8820 | 273 | __enable_irq(); |
t_tatsuoka | 0:9779b89a8820 | 274 | |
t_tatsuoka | 0:9779b89a8820 | 275 | dbg_led1 = LED_OFF; /* for debug */ |
t_tatsuoka | 0:9779b89a8820 | 276 | } |
t_tatsuoka | 0:9779b89a8820 | 277 | } |
t_tatsuoka | 0:9779b89a8820 | 278 | } |