📄 dspsgram.c
字号:
Fprintf(stderr, "preemph = %g\n", preemph); /* -S step_size */ if (!(Sflag || p_mflag)) switch (symtype("step_size")) { case ST_UNDEF: step_size = def_step_size; break; case ST_FLOAT: step_size = getsym_d("step_size"); break; default: ERROR_EXIT("Parameter \"step_size\" not of float type.") break; } step = LROUND(step_size * sf / 1000.0); if (debug_level) Fprintf(stderr, "start = %ld, nan = %ld, frame size = %ld, step = %ld\n", start, nan, frame_len, step); /* * Allocate input buffer */ data = (short *) calloc((unsigned) tr_length + 1, sizeof(short)); spsassert(data, "couldn't allocate enough memory"); /* * create, fill in, and write output file header */ oh = new_header(FT_FEA); add_source_file(oh, iname, ih); oh->common.tag = YES; (void) strcpy(oh->common.prog, ProgName); (void) strcpy(oh->common.vers, Version); (void) strcpy(oh->common.progdate, Date); oh->variable.refer = iname; add_comment(oh, get_cmd_line(argc, argv)); freq_format = SPFMT_SYM_EDGE; spec_type = SPTYP_DB; num_freqs = tr_length/2 + 1; spsassert( !init_feaspec_hd(oh, NO, freq_format, spec_type, YES, num_freqs, SPFRM_FIXED, (float *) NULL, sf, frame_len, BYTE), "Error filling FEA_SPEC header" ) *add_genhd_l("start", (long *) NULL, 1, oh) = start; *add_genhd_l("nan", (long *) NULL, 1, oh) = nan; (void) add_genhd_c("sgram_method", hd_method, strlen(hd_method), oh); *add_genhd_l("fft_order", (long *) NULL, 1, oh) = order; *add_genhd_l("fft_length", (long *) NULL, 1, oh) = tr_length; *add_genhd_l("step", (long *) NULL, 1, oh) = step; *add_genhd_e("window_type", (short *) NULL, 1, window_types, oh) = win; *add_genhd_d("pre_emphasis", (double *) NULL, 1, oh) = preemph; (void) update_waves_gen(ih, oh, (float) start, (float) step); write_header(oh, ofile); /* * allocate spectral record */ spec_rec = allo_feaspec_rec(oh, BYTE); re_spec_val = spec_rec->re_spec_val_b; if (dsp32c_is_available()) dsp_type = DSP32C_VME; else if (dsp32_is_available()) dsp_type = DSP32_FAB2; else { Fprintf(stderr,"%s: No DSP board available.\n",ProgName); exit(1); } if (debug_level) Fprintf(stderr,"dsp_type is %d\n",dsp_type); /* * start dsp program */ switch (DSP_LOCK(dsp32_wait)) { case LCKFIL_OK: break; case LCKFIL_INUSE: ERROR_EXIT("dsp board in use") break; default: case LCKFIL_ERR: ERROR_EXIT("error in trying to secure exclusive access to dsp board") break; } locked = YES; if (dsp_type == DSP32_FAB2) { if (!(setup_dsp((spect_prog=FIND_FAB2_BIN(NULL,"dspgm")), ALWAYS_LOAD))) { Fprintf(stderr, "%s: couldn't load dsp32 program %s\n", ProgName, spect_prog); EXIT(1) } dsprg(0, C_RUN, 0xffff001b); /* start prog. (it will wait for param load) */ /* * stuff spectrogram parameters to board */ dp.nfft = tr_length; dp.win = frame_len; dp.step = step; dp.ni = 1; /* yinterp */ dp.qc[0] = 0.0; /* q_thresh */ dp.qc[1] = 0.0; /* agc_ratio */ dp.qc[2] = 1.0; /*q_step */ dp.qc[3] = 0.0; /* var_ratio */ dp.qbw[0] = dp.qc[0] + 60; dp.qbw[1] = dp.qc[1]; dp.qbw[2] = dp.qc[2] * 15; dp.qbw[3] = dp.qc[3]; dp.pre = - preemph; dp.fnx = 0; /* xdith */ dp.fny = 0; /* ydith */ dp.fni = 0; /* skip dithering */ dp.fnh = 0; /* xdith * ydith */ for (i = 0; i < frame_len; i++) fwind[i] = 1.0; if (window(frame_len, fwind, fwind, win, (double *) NULL) != 0) ERROR_EXIT("Window function failed.") cnt = (frame_len + 1)/2; for (i = 0; i < cnt; i++) dp.window[i] = fwind[i]; /* Now blast the structure to the dsp32 board as a short array. This is necessary to assure that the actual transfers are shorts, since the 680xx and SPARC architectures differ and the assemblers do different things. */ p = (short *) &dp; q = (short *) sh_mem; for (i = sizeof(struct mem_dpr)/2; i-- > 0;) *q++ = *p++; dsprg(0,C_PIR); /* notify dsp32 that params are loaded */ nsamps = end - start + 1; ny = tr_length/2; /* number of points per spectrum */ GET_NX; /* number of spectra */ for (i = 0; i < 1000 && !(dsprg(0,C_PCR)&PCR_PIF); i++) { } if (i >= 1000) ERROR_EXIT("Failed to load dsp32 params.") /* nfrm is the number of frames to compute with each call to the dsp32 */ nfrm = 2048/ny; if (step*nfrm > 1020) nfrm = 1020/step; /* for input */ nfrm = nfrm << 1; /* now it's even */ dpp = (char **) malloc(sizeof(char *) * (unsigned) ny); if (!dpp) ERROR_EXIT("Can't malloc spectrogram data pointer array.\n") for (i = 0; i < ny; i++ ) { dpp[i] = malloc((unsigned) nfrm); if (!dpp[i]) ERROR_EXIT("Can't malloc spectrogram data array.\n") } cnt = tr_length + 1; p = (short *) sh_mem; *p++ = cnt; *p++ = 0; for (i = frame_len/2; i-- > 0; ) *p++ = 0; /* Center initial window on data start */ cnt -= frame_len/2 - 1; /* by padding with zeros. */ skiprec(ifile, start - 1, size_rec(ih)); actsize = get_sd_recs(data, cnt, ih, ifile); eof = actsize < cnt; nsamps = actsize; if (eof) GET_NX; q = data; while (cnt--) *p++ = *q++; dsprg(0,C_PIR); /* let dsp know that data has been sent */ for (i = 0; i < 10000 && !(dsprg(0, C_PCR) & PCR_PIF); i++) { } /* Wait for dsp32 acknowledge. */ /* resize input buffer to handle multiple frames */ data = (short *) realloc((char *) data, nfrm * (unsigned) step * sizeof(short)); spsassert(data, "couldn't reallocate memory for input buffer"); /* * main loop */ position = start; for (x = 0; x < nx; x += nfrm) { ADJ_NFRM; cnt = nfrm*step; p = (short *) sh_mem; *p++ = cnt; *p++ = nfrm; actsize = get_sd_recs(data, cnt, ih, ifile); if (actsize < cnt && !eof) { eof = YES; nsamps += x*step + actsize; GET_NX; ADJ_NFRM; } q = data; while (cnt--) *p++ = *q++; dsprg(0,C_PIR); /* let dsp know that data has been sent */ for (i = 0; i < 100000 && !(dsprg(0, C_PCR)&PCR_PIF); i++) { } /* wait for computations to finish. */ /* upload results */ q = (short *) sh_mem; nk = nfrm/sizeof(short); for (i = 0; i < ny; i++) { p = (short *)dpp[i]; for (cnt = nk; cnt--; ) *p++ = *q++; } /* write out results */ for (cnt = 0; cnt < nfrm; cnt++) { for (i = 0; i < ny; i++) re_spec_val[i] = dpp[i][cnt]; *spec_rec->tag = position; position += step; (void) put_feaspec_rec(spec_rec, oh, ofile); } } } else { /* This is a DSP32C board */ struct mem_dpr { int nfft, win, step, ni; float pre, qc[5], qbw[5]; int fnx, fny, fni, fnh; float fh[100]; float window[512]; } *dp; int *framep, *ip; if(DSP_LoadAndGo(DSP_CHIP, (spect_prog=FIND_SURF_BIN(NULL,"dspgm"))) < 0) { Fprintf(stderr,"Couldn't load DSPC32 program: %s\n",spect_prog); EXIT(1) } if(! DSP_SharedMemory) { if(!(((dsp32c_sm = dcopen("/dev/dc0mem")) > 0) && (DSP_SharedMemory = (char*)dcmap(dsp32c_sm,1+((INBUF_CHARS_32C<<1)/getpagesize()),0)))) Fprintf(stderr,"Mem map failure: dsp32c_sm=%d SM=%x\n",dsp32c_sm,DSP_SharedMemory); } dp = (struct mem_dpr*)DSP_SharedMemory; framep = (int*)DSP_SharedMemory; dp->nfft = tr_length; dp->win = frame_len; dp->step = step; dp->ni = 1; dp->qc[0] = 0.0; dp->qc[1] = 0.0; dp->qc[2] = 1.0; dp->qc[3] = 0.0; dp->qbw[0] = dp->qc[0] + 60; dp->qbw[1] = dp->qc[1]; dp->qbw[2] = dp->qc[2] * 15; dp->qbw[3] = dp->qc[3]; dp->pre = -preemph; dp->fnx = 0; dp->fny = 0; dp->fni = 0; dp->fnh = 0; for (i = 0; i < frame_len; i++) fwind[i] = 1.0; if (window(frame_len, fwind, fwind, win, (double *) NULL) != 0) ERROR_EXIT("Window function failed.") cnt = (frame_len + 1)/2; for (i = 0; i < cnt; i++) dp->window[i] = fwind[i]; if(dsp_wait(DSP_CHIP,1000, PARAM_WAIT_CODE) >= 1000) { Fprintf(stderr,"Failed to load dsp32 params.\n"); EXIT(1); } nsamps = end - start + 1; ny = tr_length/2; /* number of points per spectrum */ GET_NX; /* number of spectra */ /* nfrm will be the number of frames computed with each call to the dsp32 */ nfrm = 2048/ny; if (step*nfrm > 1020) nfrm = 1020/step; nfrm = nfrm << 1; dpp = (char **) malloc(sizeof(char *) * (unsigned) ny); if (!dpp) ERROR_EXIT("Can't malloc spectrogram data pointer array.\n") for (i = 0; i < ny; i++ ) { dpp[i] = malloc((unsigned) nfrm); if (!dpp[i]) ERROR_EXIT("Can't malloc spectrogram data array.\n") } cnt = tr_length + 1; ip=(int*)(DSP_SharedMemory + 2*sizeof(int)); /* point to shared memory */ framep[0] = cnt; framep[1] = 0; for (i = frame_len/2; i-- > 0; ) *ip++ = 0; /* Center initial window on data start */ cnt -= frame_len/2 - 1; /* by padding with zeros. */ skiprec(ifile, start - 1, size_rec(ih)); actsize = get_sd_recs(data, cnt, ih, ifile); eof = actsize < cnt; nsamps = actsize; if (eof) GET_NX; q = data; do { *ip++ = *q++; } while( --cnt); /* Sent data to the DSP32; now wait for the DSP32 to acknowledge. */ if(dsp_wait(DSP_CHIP,100000, DATA_WAIT_CODE) >= 100000) { printf("DSP32C failed to respond to data transfer.\n"); EXIT(1); } data = (short *) realloc((char *) data, nfrm * (unsigned) step * sizeof(short)); spsassert(data, "couldn't reallocate memory for input buffer"); position = step; for(x=0; x<nx; x+=nfrm) { /* main loop running dsp32c */ /* now give him data */ ADJ_NFRM; ip = (int*)(DSP_SharedMemory + 2*sizeof(int)); cnt = nfrm*step; framep[0] = cnt; framep[1] = nfrm; actsize = get_sd_recs(data, cnt, ih, ifile); if (actsize < cnt && !eof) { eof = YES; nsamps += x*step + actsize; GET_NX; ADJ_NFRM; } q = data; while (cnt--) {*ip++ = *q++; } /* Send data to the DSP32, and wait for the DSP32 to acknowledge. */ if(dsp_wait(DSP_CHIP,1000000, DATA_WAIT_CODE) >= 1000000) { printf("DSP32C failed to respond to data transfer.\n"); EXIT(1); } /* upload results */ { char *q = ((char*)DSP_SharedMemory) + 3, *cp; register int nk = 8, nl=4, j, l; for(i = 0, l=nl; i < ny; i++) for(j=nfrm, cp = dpp[i]; j--; ) { *cp++ = *q--; if(! --l ) { q += nk; l = nl; } } } /* write out results */ for (cnt = 0; cnt < nfrm; cnt++) { for (i = 0; i < ny; i++) re_spec_val[i] = dpp[i][cnt]; *spec_rec->tag = position; position += step; (void) put_feaspec_rec(spec_rec, oh, ofile); } } } /* * put info in ESPS common */ if (strcmp(oname, "<stdout>") != 0 && strcmp(iname, "<stdin>") != 0) { (void) putsym_s("filename", iname); (void) putsym_s("prog", ProgName); (void) putsym_i("start", (int) start); (void) putsym_i("nan", (int) nan); } EXIT(0) /*NOTREACHED*/ }/*************************************************************************//* * Copy of function in spect.c */#define FALSE 0#define TRUE 1intsetup_dsp(prog, ld_stat) char *prog; int ld_stat;{ static short dsp_is_open = -1;/* #define DA_RUN 1 *//* #define DA_END 2 *//* if(da_done == DA_RUN) dsprg(0,C_CLRSIG); */ /* clear pending interrupt *//* da_done = DA_END; */ if(dsp_is_open < 0) { dsp_is_open = dspopn((char*)0,0); sh_mem = dspmap(); } dsprg(0, C_WRITE|C_CSR, 0x6a); dsprg(0, C_WRITE|C_CSR, 0xea); /* remove reset; set mem mode bit; disable interrupts */ /* Halt all three dsp32's */ dsprg(0,C_STOP); dsprg(1,C_STOP); dsprg(2,C_STOP); dsprg(0,C_WRITE|C_PCR,0x18); dsprg(1,C_WRITE|C_PCR,0x18); dsprg(2,C_WRITE|C_PCR,0x18); if((ld_stat == ALWAYS_LOAD) || strcmp(loaded, prog)) { if(dspld(0,prog) < 0) { return(FALSE); } (void) strcpy(loaded,prog); } else dsprg(0,C_WRITE|C_PCR,0x1a); /* simply halt the dsp32 */ return(TRUE);}/*************************************************************************//* * For debug printout of float arrays */void pr_farray(text, n, arr) char *text; long n; float *arr;{ int i; Fprintf(stderr, "%s: %s -- %d points:\n", ProgName, text, n); for (i = 0; i < n; i++) { Fprintf(stderr, "%f ", arr[i]); if (i%5 == 4 || i == n - 1) Fprintf(stderr, "\n"); }}dsp32c_is_available(){ int dc2; int dc = open("/dev/dc00",O_RDWR,0); if(dc > 0) { close(dc); dc2 = dcopen("/dev/dc00"); if(dc2 > 0) { close(dc2); return(1); } } return(0);}dsp32_is_available(){ int didopen; if(dspopn((char*) 0,0) < 0) { return(0); } else return(1);}/*************************************************************************/DSP_LoadAndGo (chip, program) int chip; char *program;{ char dsname[200]; sprintf(dsname,"/dev/dc0%d",chip); if((dsp32c[chip] < 0) && ((dsp32c[chip] = dcopen(dsname)) < 0)) { printf("Couldn't open %s\n",dsname); return(-1); } if (ioctl(dsp32c[chip], DC_RESET, (char*)NULL) != 0) { perror ("Couldn't reset the board in DSP_LoadAndGo()\n"); exit(5); } dcinit(dsp32c[chip], 0); dcld(dsp32c[chip], program); dcrun(dsp32c[chip]); if(dsp_wait(chip,1000,DSP_START_CODE) >= 1000) printf("Load and go failed by timeout\n"); return(dsp32c[chip]);}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -