Compare commits

...

5 Commits

Author SHA1 Message Date
Borshin Chang 262757d609 Central_NeuLive: thres with division
10Hz and 20Hz cannot separate
     0.5mV    freq:(1 5 10 20 40 50)Hz = LED(0 0 1 1 0 0)
BUT  1-mV     freq:(2.5 4 5)Hz , LED=0
     0.375-mV freq:(7.5  15)Hz , LED=0
2020-12-02 19:08:38 +08:00
Borshin Chang 973f57f757 Central_NeuLive: thres>300k,
0.5mV    freq:(1 5 10 20 40 50)Hz = LED(0 0 1 0 0 0)
BUT  1-mV     freq:(2.5 4 5)Hz , LED=1
     0.375-mV freq:(7.5  15)Hz , LED=1
2020-12-02 16:02:19 +08:00
Borshin Chang 05d36a6f1e 12/2 before taking cali value 2020-12-02 14:11:49 +08:00
Borshin Chang fe977541f2 64-point FFT, then detect power spectrum of data, with BIR change 7 bit -> 6bit
by the way data now can be really 000~FFF then ADC_unsigned_to_signed
then go FFT
2020-12-01 18:07:55 +08:00
Borshin Chang fb543aa7b4 64-point FFT, then detect power spectrum of data,
by the way data now can be really 000~FFF then ADC_unsigned_to_signed
then go FFT
2020-12-01 17:44:54 +08:00
2 changed files with 109 additions and 75 deletions
@@ -4,7 +4,7 @@
// define Device_Name if "BOARD_TEST" as default
// else find headstage ID
#define BOARD_18_04_ED_37_C6_52
#define BOARD_18_04_ED_37_C6_41
typedef struct _SingleChannelCali{
uint16_t Gain[4];
@@ -13,17 +13,33 @@
#include "headstage_cali_table.h"
static uint8_t numFFT = 16; // user define
static uint8_t step = 8; // which is numFFT/2
static uint8_t stageFFT = 4; // which is log2(numFFT)
static int twiddle_real[16] = {10000, 9238, 7071, 3826, 0, -3827, -7072, -9239, -10000, -9239, \
-7072, -3827, -1, 3826, 7071, 9238};
static uint8_t numFFT = 64; // user define
static uint8_t step = 32; // which is numFFT/2
static uint8_t stageFFT = 6; // which is log2(numFFT)
//static int twiddle_real[16] = {10000, 9238, 7071, 3826, 0, -3827, -7072, -9239, -10000, -9239, \
// -7072, -3827, -1, 3826, 7071, 9238};
//
//static int twiddle_imag[16] = { 0, -3827, -7072, -9239, -10000, -9239, -7072, -3827, -1, 3826, \
// 7071, 9238, 10000, 9238, 7071, 3826};
static int twiddle_real[64] = {10000, 9951, 9807, 9569, 9238, 8819, 8314, 7730, 7071, 6343, \
5555, 4713, 3826, 2902, 1950, 980, 0, -981, -1951, -2903, \
-3827, -4714, -5556, -6344, -7072, -7731, -8315, -8820, -9239, -9570, \
-9808, -9952, -10000, -9952, -9808, -9570, -9239, -8820, -8315, -7731, \
-7072, -6344, -5556, -4714, -3827, -2903, -1951, -981, -1, 980,\
1950, 2902, 3826, 4713, 5555, 6343, 7071, 7730, 8314, 8819, \
9238, 9569, 9807, 9951 };
static int twiddle_imag[64] = { 0, -981, -1951, -2903, -3827, -4714, -5556, -6344, -7072, -7731, \
-8315, -8820, -9239, -9570, -9808, -9952, -10000, -9952, -9808, -9570, \
-9239, -8820, -8315, -7731, -7072, -6344, -5556, -4714, -3827, -2903, \
-1951, -981, -1, 980, 1950, 2902, 3826, 4713, 5555, 6343, \
7071, 7730, 8314, 8819, 9238, 9569, 9807, 9951, 10000, 9951, \
9807, 9569, 9238, 8819, 8314, 7730, 7071, 6343, 5555, 4713, \
3826, 2902, 1950, 980};
static int twiddle_imag[16] = { 0, -3827, -7072, -9239, -10000, -9239, -7072, -3827, -1, 3826, \
7071, 9238, 10000, 9238, 7071, 3826};
static void FFT(int* pred_LFP, int* imag_LFP, int numFFT);
static void BitReverse(int* BR_Array, int numFFT);
static void CLP(uint16_t raw_data);
static void CLP(int raw_data);
static int ADC_U2S( uint16_t unsigned_buff);
@@ -63,29 +79,40 @@ static void headstage_neu_append_notify_data() {
// illegal channel
return;
}
uint16_t borshin_raw;
int16_t borshin_a;
int16_t borshin_b;
uint8_t borshin_sample_rate;
int borshin_real;
int borshin_compute_data;
static uint8_t borshin_sample_count;
uint8_t not_buf[2];
not_buf[0] = (channel & 0x0F) << 4 | (spi_rxbuf[1] & 0x0F);
not_buf[1] = spi_rxbuf[2];
// Borshin closed-loop start here <<<<<<<<<<<<<<<<<<<<---->>>>>>>>>>>>>>>>>>>>----------------------
uint16_t borshin_raw;
uint8_t borshin_a;
uint8_t borshin_b;
uint8_t borshin_sample_rate;
uint16_t borshin_real;
borshin_raw = (spi_rxbuf[1]&0x0F)<<8 | spi_rxbuf[2];
borshin_a = CaliTable.Ch[channel].Gain[ rec_register_value[REC_CHANNEL_INDEX] ];
borshin_b = CaliTable.Ch[channel].Offset[ rec_register_value[REC_CHANNEL_INDEX] ];
borshin_real = borshin_raw * borshin_a + borshin_b; // range from [ FFF , 000 ]
borshin_sample_rate = 2000000/10/rec_register_value[REC_ADC_CLOCK_INDEX];
CLP(borshin_real); // This function input is 12-bit value
// the function will buffer 16-point then
// perform 16-point FFT.
// Afterwards, calculate sum of PSD then
// decided if HVS happened. (GPIO_4)
// Borshin closed-loop END here <<<<<<<<<<<<<<<<<<<<----------------------------->>>>>>>>>>>>>>>>>>>>
// Borshin FFT start here <<<<<<<<<<<<<<<<<<<<---->>>>>>>>>>>>>>>>>>>>----------------------
borshin_sample_count = !borshin_sample_count;
if (borshin_sample_count){
borshin_raw = (spi_rxbuf[1]&0x0F)<<8 | spi_rxbuf[2]; // range from [ FFF , 000 ]
borshin_a = CaliTable.Ch[channel].Gain[ rec_register_value[REC_CHANNEL_INDEX] ];
borshin_b = CaliTable.Ch[channel].Offset[ rec_register_value[REC_CHANNEL_INDEX] ];
borshin_real = (borshin_raw - 2048 - borshin_b)*1000/borshin_a; // range from +- someting [ no FFF , 000 ]
borshin_compute_data = borshin_real >> 10;
borshin_sample_rate = 2000000/10/rec_register_value[REC_ADC_CLOCK_INDEX];
// if (borshin_raw<3450&&borshin_raw>600)
// headstage_pin_output(PIN_CLP, 1);
// else
// headstage_pin_output(PIN_CLP, 0);
CLP( borshin_compute_data ); // This function input is 12-bit value
// the function will buffer 16-point then
// perform 16-point FFT.
// Afterwards, calculate sum of PSD then
// decided if HVS happened. (GPIO_4)
// Borshin FFT END here <<<<<<<<<<<<<<<<<<<<----------------------------->>>>>>>>>>>>>>>>>>>>
}
uint8_t data_size = headstage_notify_append_data(not_buf);
if (data_size >= BLE_NOT_BUFF_SIZE) {
@@ -234,18 +261,33 @@ static void BitReverse(int* BR_Array, int numFFT){
uint8_t n=0;
int tmp;
// for (uint8_t i=0; i<numFFT; i++){
// n = i; // printf ("%d %d\n",i,n);
// n = (n&0x0f)<<4 | (n&0xf0)>>4; // 0123_4567 -> 4567_0123
// n = (n&0x33)<<2 | (n&0xcc)>>2; // 4567_0123 -> 6745_2301
// n = (n&0xaa)<<2 | (n&0x55)>>1; // 7856_3412 -> 7654_3210
// n = n >> 1; // 7654_3210 -> 0765_4321
// if (n>i){
// tmp = BR_Array[n];
// BR_Array[n] = BR_Array[i];
// BR_Array[i] = tmp;
// }
// }
// This BIR specify for 6-bit (0000_0000 ~ 0011_1111)
for (uint8_t i=0; i<numFFT; i++){
n = i; // printf ("%d %d\n",i,n);
n = (n&0x0f)<<4 | (n&0xf0)>>4; // 0123_4567 -> 4567_0123
n = (n&0x33)<<2 | (n&0xcc)>>2; // 4567_0123 -> 6745_2301
n = (n&0xaa)<<2 | (n&0x55)>>1; // 7856_3412 -> 7654_3210
n = n >> 1; // 7654_3210 -> 0765_4321
n = (n&0x07)<<3 | (n)>>3; // 0012_3456 -> 0045_6123
n = (n&0x09)<<2 | (n&0x36)>>1; // 0045_6123 -> 0064_5312
n = (n&0x09)<<1 | (n&0x12)>>1 | (n&0x24); // 0064_5312 -> 0065_4321
if (n>i){
tmp = BR_Array[n];
BR_Array[n] = BR_Array[i];
BR_Array[i] = tmp;
}
}
return;
} // END Bit Reverse Order Sort
@@ -258,61 +300,53 @@ static void BitReverse(int* BR_Array, int numFFT){
*
* @return None.
*/
static void CLP(uint16_t unsigned_real_data){
static void CLP(int signed_real_data){
int signed_buff;
static int accumu_sum;
static int x_in[16];
static int real[16];
static int imag[16];
static int count_sample;
static int count_data;
static int x_in[64];
static int real[64];
static int imag[64];
static int count;
int abs_tmp;
static int present, past1, past2, hvs;
static int w[6] = {1597,-1909,1856,-1190,630,23};
count_sample++; // count+1 every 1-ms
if (count_sample>=24){ // ensure sample LFP data every 24-ms(41.6667Hz)
signed_buff = ADC_U2S(unsigned_real_data); // FFF ~ 000 to +7FF ~ -7FF
x_in[count_data] = signed_buff;
real[count_data] = signed_buff;
imag[count_data] = 0;
count_sample = 0; // re-calculate count
count_data++;
if (count_data>=6){
accumu_sum = 0;
for (int i=6;i<numFFT;i++){
real[i] = ( w[0] * real[i-1] \
+ w[1] * real[i-2] \
+ w[2] * real[i-3] \
+ w[3] * real[i-4] \
+ w[4] * real[i-5] \
+ w[5] * real[i-6] )>>10;
imag[i] = 0;
}
BitReverse( real, numFFT);
FFT( real, imag, numFFT);
for (int i=1;i<6;i++){
if (i>=2&&i<=4){
accumu_sum += real[i]*real[i]+imag[i]*imag[i];
// signed_buff = ADC_U2S(unsigned_real_data);
x_in[count] = signed_real_data;
real[count] = signed_real_data;
imag[count] = 0;
count++;
if (count >= numFFT){
BitReverse( real, numFFT);
FFT( real, imag, numFFT);
abs_tmp=0;
accumu_sum = 0;
for ( int i=0; i<numFFT; i++){
accumu_sum = accumu_sum + (real[i]*real[i]+imag[i]*imag[i]);
if (i>0){
if (i==1){
abs_tmp = real[i]*real[i]+imag[i]*imag[i];
}
real[i-1] = x_in[i];
imag[i-1] = 0;
x_in[i-1] = x_in[i];
}
present = ( (accumu_sum>>12) > 45000 )?1:0;
if (present+past1+past2==3)
hvs = 1;
else
hvs = 0;
past2 = past1;
past1 = present;
if (hvs==1)
headstage_pin_output(PIN_CLP, 1);
else
headstage_pin_output(PIN_CLP, 0);
count_data--;
}
int flag;
flag = abs_tmp*100/(accumu_sum>>1);
if (flag > 62 && flag < 65 ){
headstage_pin_output(PIN_CLP, 1);
}
else {
headstage_pin_output(PIN_CLP, 0);
}
// if (abs_tmp > 300000 ){
// headstage_pin_output(PIN_CLP, 1);
// }
// else {
// headstage_pin_output(PIN_CLP, 0);
// }
count--;
}
return;
}