/* ** Function NAME: downsample */ void matchedfilter::downsample(int downsample_factor, MEMORY mem_buffer) { if (downsample_factor <= 1) return; float l_sampling_freq; float *workspace = mem_buffer.allocate_float( 2 * downsample_factor ); l_sampling_freq = down_sample (ref_ax, samp_freq_ref, time_window_ref, downsample_factor, workspace); l_sampling_freq = down_sample (ref_ay, samp_freq_ref, time_window_ref, downsample_factor, workspace); samp_freq_ref = l_sampling_freq; N_window_ref /= downsample_factor; dt_ref *= downsample_factor; N_data_ref /= downsample_factor; for (int k = N_window_ref; k < N_data_ref; k++) ref_ax[k] = 0.0f; for (int k = N_window_ref; k < N_data_ref; k++) ref_ay[k] = 0.0f; }
// This is the entry_point for the encode function, and will make sure the result // sane. xyuv::frame encode_frame(const xyuv::yuv_image &yuva_in, const xyuv::format &format) { bool dimensions_match = yuva_in.image_w == format.image_w && yuva_in.image_h == format.image_h; // Short path. if (dimensions_match && yuva_in.siting == format.chroma_siting) { return internal_encode_frame(yuva_in, format); } // Otherwise we will need to do some conversion. const yuv_image *image = &yuva_in; yuv_image temp; if (!dimensions_match) { if (!is_444(image->siting.subsampling)) { temp = up_sample(*image); image = &temp; } temp = scale_yuv_image(*image, format.image_w, format.image_h); image = &temp; } if (!(image->siting == format.chroma_siting)) { if (image->siting.subsampling.macro_px_w > 1 || image->siting.subsampling.macro_px_h > 1) { temp = up_sample(*image); image = &temp; } // At this point *image is 444 if (format.chroma_siting.subsampling.macro_px_w > 1 || format.chroma_siting.subsampling.macro_px_h > 1) { temp = down_sample(*image, format.chroma_siting); image = &temp; } } return internal_encode_frame(*image, format); }
int main(int argc, char **argv) { int c; int inflag = 0; int dbg = 0; int sr_ds = 200; char * rec_name = "vfdb/427"; char * db_path = "/opt/physiobank/database"; size_t win_sec = 8; /* r stands for record with folder * p for path * i for information of record * s downsample sr * w window length default:8 * d debug */ while ((c = getopt(argc, argv, "idr:p:s:w:")) != -1) switch (c){ case 'i': inflag = 1; break; case 'r': rec_name = optarg; break; case 'p': db_path = optarg; break; case 's': sr_ds= atoi(optarg); break; case'w': win_sec = atoi(optarg); break; case'd': dbg = 1; break; default: abort(); } int i, j, nsig; WFDB_Sample *v; WFDB_Siginfo *s; WFDB_Anninfo a; setwfdb(db_path); nsig = isigopen(rec_name, NULL, 0); if (nsig < 1){ printf("nsig:%d\n",nsig); exit(1); } s = (WFDB_Siginfo *)malloc(nsig * sizeof(WFDB_Siginfo)); if (isigopen(rec_name, s, nsig) != nsig) exit(1); v = (WFDB_Sample *)malloc(nsig * sizeof(WFDB_Sample)); int orig_sr = sampfreq(rec_name); a.name = "atr"; a.stat = WFDB_READ; if (wfdbinit(rec_name, &a, 1, s, nsig) != nsig) exit(3); if(1 == inflag ){ printf("sr:%d\n",orig_sr); printf("%d signals\n", nsig); for (i = 0; i < nsig; i++) { printf("Group %d, Signal %d:\n", s[i].group, i); printf("File: %s\n", s[i].fname); printf("Description: %s\n", s[i].desc); printf("Gain: "); if (s[i].gain == 0.) printf("uncalibrated; assume %g", WFDB_DEFGAIN); else printf("%g", s[i].gain); printf(" adu/%s\n", s[i].units ? s[i].units : "mV"); printf(" Initial value: %d\n", s[i].initval); printf(" Storage format: %d\n", s[i].fmt); printf(" I/O: "); if (s[i].bsize == 0) printf("can be unbuffered\n"); else printf("%d-byte blocks\n", s[i].bsize); printf(" ADC resolution: %d bits\n", s[i].adcres); printf(" ADC zero: %d\n", s[i].adczero); if (s[i].nsamp > 0L) { printf(" Length: %s (%ld sample intervals)\n", timstr(s[i].nsamp), s[i].nsamp); printf(" Checksum: %d\n", s[i].cksum); } else printf(" Length undefined\n"); } } fifo_t fifo_ecg; int fifo_ecg_buf[FIFO_SIZE]; fifo_init(&fifo_ecg, fifo_ecg_buf, FIFO_SIZE); fifo_t fifo_bt; int fifo_bt_buf[FIFO_SIZE]; fifo_init(&fifo_bt, fifo_bt_buf, FIFO_SIZE); int tmp = 0; int sr = 200; WFDB_Time begin_samp = 0; WFDB_Time end_samp = orig_sr*win_sec; WFDB_Annotation begin_ann; WFDB_Annotation end_ann; getann(0, &begin_ann); while(0 == getann(0, &end_ann)) if ((end_ann.aux != NULL && *end_ann.aux > 0) ||0 == strcmp(annstr(end_ann.anntyp), "[") ||0 == strcmp(annstr(end_ann.anntyp), "]") ||0 == strcmp(annstr(end_ann.anntyp), "~") ){ break; } int * pBt_len = (int*)calloc(win_sec,sizeof(int)); ResetBDAC(); for (; ;) { if (getvec(v) < 0) break; // for (j = 0; j < nsig; j++){ // } tmp = v[nsig-1]; tmp = v[0]; int vout1 = 0; static int bt_i = 0; static unsigned int samplecnt = 0; int idx = 0; if(down_sample(tmp, &vout1, orig_sr, sr)) { samplecnt ++; fifo_write(&fifo_ecg, &vout1, 1*sizeof(int)); int beatType, beatMatch; long ltmp = vout1-s[0].adczero; ltmp *= 200; ltmp /= s[0].gain; int bdac_dly = BeatDetectAndClassify(ltmp, &beatType, &beatMatch); idx = bt_i/sr; if (0 != bdac_dly ) { pBt_len[idx]++; fifo_write(&fifo_bt, &beatType, sizeof(int)); } bt_i = ++bt_i%(win_sec*sr); } double cm = 0.0; int size = win_sec*sr; if(fifo_len(&fifo_ecg)/sizeof(int) >= sr*win_sec){ int * win_data = (int*)malloc(win_sec*sr*sizeof(int)); int * ds_data = (int*)malloc(win_sec*sr_ds*sizeof(int)); int len = fifo_len(&fifo_bt)/sizeof(int); int * p = (int*)calloc(len, sizeof(int)); if (0 != len){ //printf("bt_i:%d\n", bt_i); fifo_read_steps(&fifo_bt, p, len*sizeof(int), pBt_len[bt_i/sr]*sizeof(int)); //for (i = 0;i< len;i++) printf("%d ", p[i]); //printf("\n"); } pBt_len[bt_i/sr] = 0; fifo_read_steps(&fifo_ecg, win_data, size*sizeof(int), sr*sizeof(int)); filtering(win_data, size, sr); int i = 0; int ds_size = 0; int vout; for(i = 0;i < size;i++){ // if(down_sample(win_data[i], &vout, sr, sr_ds)) ds_data[ds_size++] = vout; } double dven = 0.0; if (0 != len){ int tmp_cnt = 0; for(i = 0; i < len ; i++){ if(5 == p[i]) dven ++; } //printf("div:%d %d\n", tmp_cnt, len); dven /= len; } //cm = ecg_complexity_measure(win_data, size); //cm = ecg_complexity_measure(ds_data, ds_size); //cm = cpsd(ds_data, ds_size, 0.5*sr_ds); //cm = calc_grid(ds_data, ds_size, 0.5*sr_ds); cm = calc_grid(win_data, size, 0.5*sr); if (-1 == cm ) continue; //VT print 1; //VF print 2; int hr = (int)((double)(len*60)/win_sec+0.5); int ret = check_ann(begin_samp, end_samp, &begin_ann, &end_ann, "(VT"); if (1 == ret){ printf("%d %lf %lf %d\n", 1, cm, dven, hr); } else if (0 == ret) { ret = check_ann(begin_samp, end_samp, &begin_ann, &end_ann, "(VFL"); int ret2 = check_ann(begin_samp, end_samp, &begin_ann, &end_ann, "["); if (1 == ret || 1 == ret2){ printf("%d %lf %lf %d\n", 2, cm, dven, hr); } else if (0 == ret){ // ret = check_ann(begin_samp, end_samp, &begin_ann, &end_ann, "(N"); // int ret3 = check_ann(begin_samp, end_samp, &begin_ann, &end_ann, "N"); // if ((1 == ret || 1 == ret3) && -1 != begin_ann.subtyp) printf ("%d %lf %lf %d\n", 0, cm, dven, hr); if (1 == check_ann(begin_samp, end_samp, &begin_ann, &end_ann, "(AFIB") || 1 == check_ann(begin_samp, end_samp, &begin_ann, &end_ann, "(AFL") || 1 == check_ann(begin_samp, end_samp, &begin_ann, &end_ann, "(IVR") || 1 == check_ann(begin_samp, end_samp, &begin_ann, &end_ann, "(SVTA") || 1 == check_ann(begin_samp, end_samp, &begin_ann, &end_ann, "(SBR") || 1 == check_ann(begin_samp, end_samp, &begin_ann, &end_ann, "(BII") ) printf ("%d %lf %lf %d\n", 0, cm, dven, hr); // int ret = check_ann(begin_samp, end_samp, &begin_ann, &end_ann, "(SVTA"); // if (1 == ret) printf ("%d %lf\n", 0, cm); } } //iannsettime(begin_samp); begin_samp += orig_sr; end_samp += orig_sr; if(end_ann.time < begin_samp){ begin_ann = end_ann; while(1) if (0 != getann(0, &end_ann)){ end_ann = begin_ann; /*the last sample of the signal*/ end_ann.time = s[nsig-1].nsamp; break; }else if ((end_ann.aux != NULL && *end_ann.aux > 0) ||0 == strcmp(annstr(end_ann.anntyp), "[") ||0 == strcmp(annstr(end_ann.anntyp), "]") ||0 == strcmp(annstr(end_ann.anntyp), "~") ){ break; } if(dbg){ printf("begin tm:%s type:%s ", mstimstr(begin_ann.time), annstr(begin_ann.anntyp)); if(begin_ann.aux != NULL) printf("begin aux:%s", begin_ann.aux+1); printf("\n"); printf("end tm:%s type:%s ", mstimstr(end_ann.time), annstr(end_ann.anntyp)); if (end_ann.aux != NULL) printf("end aux:%s", end_ann.aux+1); printf("\n"); printf("begin sample:%s\n", mstimstr(begin_samp)); printf("ann diff:%s\n", mstimstr(end_ann.time-begin_ann.time)); } } free(p); free(ds_data); free(win_data); } } free(pBt_len); wfdbquit(); return 0; }