takuya okada
/
test
疑似的な脈波・呼吸・いびきを出力するプログラムです。
Diff: main.cpp
- Revision:
- 0:63339e4deacf
- Child:
- 1:97c5a956a371
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/main.cpp Tue Nov 08 02:43:21 2016 +0000 @@ -0,0 +1,266 @@ +#include "mbed.h" +#include "math.h" + +AnalogOut signal(p18); + +float sample_freq; +float hb_carr_freq; +float hb_mod_freq; +float hb_carr_level; +float hb_mod_ratio; +float resp_freq; +float resp_level; +float snore_freq; +float snore_level; +int snore_oncycle; +int snore_allcycle; //snore_oncycle < snore_cycle; + +float coswave[257]; + + +int main() { + + + +sample_freq = 10000; +hb_carr_freq = 10; +hb_mod_freq = 2; +hb_carr_level = 0.005; +hb_mod_ratio = 100; +resp_freq = 0.3; +resp_level = 0.01; +snore_freq = 150; +snore_level = 0.003; +snore_oncycle = 10000; +snore_allcycle = 30000; + + +// coswave + int i; + + for(i=0;i<=256;i++){ + coswave[i]=0.5*(cos(2.0*3.1415*i/256)); + } + i = 0; + +// sampling + float sample_dt; + sample_dt = 1/sample_freq; + +// hb_carr + float hb_carr_period; + hb_carr_period = 1/hb_carr_freq; + + float hb_carr_n; + hb_carr_n = 1; + + float hb_carr_n_sample; //The maximum number of sampling points in the signal + float hb_carr_tend = 0; //The remainder of sampling points in the signal + float hb_carr_tstart = 0; //start offset of sampling time + float hb_carr_t_samplepoint; // + + float hb_carr_table_dt; + hb_carr_table_dt = hb_carr_period/256; + + float hb_carr_n_samplepoint; + float hb_carr_rem_samplepoint; + float hb_carr_v_samplepoint; + +// hb_mod + float hb_mod_period; + hb_mod_period = 1/hb_mod_freq; + + float hb_mod_n; + hb_mod_n = 1; + + float hb_mod_n_sample; //The maximum number of sampling points in the signal + float hb_mod_tend = 0; //The remainder of sampling points in the signal + float hb_mod_tstart = 0; //start offset of sampling time + float hb_mod_t_samplepoint; // + + float hb_mod_table_dt; + hb_mod_table_dt = hb_mod_period/256; + + float hb_mod_n_samplepoint; + float hb_mod_rem_samplepoint; + float hb_mod_v_samplepoint; + + float hb_mod_ra; + hb_mod_ra = hb_mod_ratio / 100; + + float hb_signal; + +// resp + float resp_period; + resp_period = 1/resp_freq; + + float resp_n; + resp_n = 1; + + float resp_n_sample; //The maximum number of sampling points in the signal + float resp_tend = 0; //The remainder of sampling points in the signal + float resp_tstart = 0; //start offset of sampling time + float resp_t_samplepoint; // + + float resp_table_dt; + resp_table_dt = resp_period/256; + + float resp_n_samplepoint; + float resp_rem_samplepoint; + float resp_v_samplepoint; + +// snore + float snore_period; + snore_period = 1/snore_freq; + + float snore_n; + snore_n = 1; + + float snore_n_sample; //The maximum number of sampling points in the signal + float snore_tend = 0; //The remainder of sampling points in the signal + float snore_tstart = 0; //start offset of sampling time + float snore_t_samplepoint; // + + float snore_table_dt; + snore_table_dt = snore_period/256; + + float snore_n_samplepoint; + float snore_rem_samplepoint; + float snore_v_samplepoint; + + int snore_cycle; + snore_cycle = 0; + +// etc. + + float signalout; + + + while(1){ + + //hb section + //hb_carr + + hb_carr_n_sample = (hb_carr_period - hb_carr_tstart) / sample_dt; + hb_carr_tend = fmod((hb_carr_period - hb_carr_tstart) , sample_dt); + + hb_carr_t_samplepoint = hb_carr_tstart + sample_dt * hb_carr_n; //time of sampling point + + hb_carr_n_samplepoint = hb_carr_t_samplepoint / hb_carr_table_dt; + hb_carr_rem_samplepoint = fmod(hb_carr_t_samplepoint , hb_carr_table_dt); + + hb_carr_v_samplepoint = hb_carr_level*((hb_carr_rem_samplepoint/hb_carr_table_dt)*(coswave[(int)hb_carr_n_samplepoint+1]-coswave[(int)hb_carr_n_samplepoint])+coswave[(int)hb_carr_n_samplepoint]); + +// signal = hb_carr_v_samplepoint+0.5; //0-1 -> 0V-3.3V +// printf("V= %f \n",hb_carr_v_samplepoint); + + if(hb_carr_n == (int)hb_carr_n_sample) { + hb_carr_n =1; + hb_carr_tstart = sample_dt - hb_carr_tend; + } + + else { + hb_carr_n++; + } + //hb_mod + + hb_mod_n_sample = (hb_mod_period - hb_mod_tstart) / sample_dt; + hb_mod_tend = fmod((hb_mod_period - hb_mod_tstart) , sample_dt); + + hb_mod_t_samplepoint = hb_mod_tstart + sample_dt * hb_mod_n; //time of sampling point + + hb_mod_n_samplepoint = hb_mod_t_samplepoint / hb_mod_table_dt; + hb_mod_rem_samplepoint = fmod(hb_mod_t_samplepoint , hb_mod_table_dt); + + hb_mod_v_samplepoint = hb_mod_ra*((hb_mod_rem_samplepoint/hb_mod_table_dt)*(coswave[(int)hb_mod_n_samplepoint+1]-coswave[(int)hb_mod_n_samplepoint])+coswave[(int)hb_mod_n_samplepoint]); + +// signal = hb_mod_v_samplepoint+0.5; //0-1 -> 0V-3.3V +// printf("V= %f \n",hb_mod_v_samplepoint); + + if(hb_mod_n == (int)hb_mod_n_sample) { + hb_mod_n =1; + hb_mod_tstart = sample_dt - hb_mod_tend; + } + + else { + hb_mod_n++; + } + + //hb_AM MOD + hb_signal = hb_carr_v_samplepoint + hb_mod_v_samplepoint*hb_carr_v_samplepoint; + +// signal = hb_signal+0.5; + + //resp section + + resp_n_sample = (resp_period - resp_tstart) / sample_dt; + resp_tend = fmod((resp_period - resp_tstart) , sample_dt); + + resp_t_samplepoint = resp_tstart + sample_dt * resp_n; //time of sampling point + + resp_n_samplepoint = resp_t_samplepoint / resp_table_dt; + resp_rem_samplepoint = fmod(resp_t_samplepoint , resp_table_dt); + + resp_v_samplepoint = resp_level*((resp_rem_samplepoint/resp_table_dt)*(coswave[(int)resp_n_samplepoint+1]-coswave[(int)resp_n_samplepoint])+coswave[(int)resp_n_samplepoint]); + +// signal = resp_v_samplepoint+0.5; //0-1 -> 0V-3.3V +// printf("V= %f \n",resp_v_samplepoint); + + if(resp_n == (int)resp_n_sample) { + resp_n =1; + resp_tstart = sample_dt - resp_tend; + } + + else { + resp_n++; + + } + + //snore section + + snore_n_sample = (snore_period - snore_tstart) / sample_dt; + snore_tend = fmod((snore_period - snore_tstart) , sample_dt); + + snore_t_samplepoint = snore_tstart + sample_dt * snore_n; //time of sampling point + + snore_n_samplepoint = snore_t_samplepoint / snore_table_dt; + snore_rem_samplepoint = fmod(snore_t_samplepoint , snore_table_dt); + + snore_v_samplepoint = snore_level*((snore_rem_samplepoint/snore_table_dt)*(coswave[(int)snore_n_samplepoint+1]-coswave[(int)snore_n_samplepoint])+coswave[(int)snore_n_samplepoint]); + +// signal = snore_v_samplepoint+0.5; //0-1 -> 0V-3.3V +// printf("V= %f \n",snore_v_samplepoint); + + if(snore_n == (int)snore_n_sample) { + snore_n =1; + snore_tstart = sample_dt - snore_tend; + } + + else { + snore_n++; + + } + + if (snore_cycle <= snore_oncycle) { + snore_cycle++; + } + else if (snore_cycle>snore_oncycle && snore_cycle <= snore_allcycle){ + snore_v_samplepoint = 0; + snore_cycle++; + } + else { + snore_cycle = 0; + } + + + signalout = (hb_signal + resp_v_samplepoint + snore_v_samplepoint); + signal = signalout +0.5; //0-1 -> 0V-3.3V + + wait(sample_dt); + +char msg[10]; +printf(msg, "%d", SystemCoreClock ); + + + } +}