main() { int npoles,nfft,i; float f3db,zero=0.0,fpass,apass,fstop,astop; /* printf("Enter npoles f3db:\n"); scanf("%d %f",&npoles,&f3db); */ printf("Enter fpass apass fstop astop:\n"); scanf("%f %f %f %f",&fpass,&apass,&fstop,&astop); bfdesign(fpass,apass,fstop,astop,&npoles,&f3db); printf("npoles = %d f3db = %f\n",npoles,f3db); /* impulse response */ scopy(N,&zero,0,p,1); p[0] = 1.0; bflowpass(npoles,f3db,N,p,q); pp1d(stdout,"impulse response",N,0,q); /* amplitude spectrum */ nfft = npfa(N); for (i=0; i<N; i++) z[i] = cmplx(q[i],0.0); for (i=N; i<nfft; i++) z[i] = cmplx(0.0,0.0); pfacc(1,nfft,z); for (i=0; i<nfft; i++) zamp[i] = fcabs(z[i]); pp1d(stdout,"amplitude spectrum",nfft/2+1,0,zamp); }
void antialias (float frac, int phase, int n, float p[], float q[]) /***************************************************************************** Anti-alias filter - use before increasing the sampling interval (sub-sampling) ****************************************************************************** Input: frac current sampling interval / future interval (should be <= 1) phase =0 for zero-phase filter; =1 for minimum-phase filter n number of samples p array[n] of input samples Output: q array[n] of output (anti-alias filtered) samples ****************************************************************************** Notes: The anti-alias filter is a recursive (Butterworth) filter. For zero-phase anti-alias filtering, the recursive filter is applied forwards and backwards. ****************************************************************************** Author: Dave Hale, Colorado School of Mines, 06/06/90 *****************************************************************************/ { int i,j,npoles,ntemp; float fnyq,fpass,apass,fstop,astop,f3db,*ptemp,ptempi; /* if no anti-alias filter need be applied, then simply copy input */ if (ABS(frac)>=1.0) { for (i=0; i<n; ++i) q[i] = p[i]; return; } /* determine number of poles and -3db point for filter */ fnyq = 0.5*ABS(frac); fpass = 0.6*fnyq; apass = 0.99; fstop = fnyq; astop = 0.01; bfdesign(fpass,apass,fstop,astop,&npoles,&f3db); /* if minimum-phase, then use npoles*2 poles in one direction only */ if (phase!=0) { bflowpass(npoles*2,f3db,n,p,q); /* else, if zero-phase, use npoles in both directions */ } else { /* pad input with zeros to catch recursive filter tail */ ntemp = n+100; ptemp = alloc1float(ntemp); for (i=0; i<n; ++i) ptemp[i] = p[i]; for (i=n; i<ntemp; ++i) ptemp[i] = 0.0; /* filter zero-padded input */ bflowpass(npoles,f3db,ntemp,ptemp,ptemp); /* reverse filtered input and filter again */ for (i=0,j=ntemp-1; i<j; ++i,--j) { ptempi = ptemp[i]; ptemp[i] = ptemp[j]; ptemp[j] = ptempi; } bflowpass(npoles,f3db,ntemp,ptemp,ptemp); /* undo the reverse while copying to output */ for (i=0,j=ntemp-1; i<n; ++i,--j) q[i] = ptemp[j]; free1float(ptemp); } }
int main(int argc, char *argv[]) { int opt; char *optstring="dhs:"; int sfd, fdmax, ret; fd_set readfds, rfds; struct timeval timeout; struct tm *gmp, *locp; int i, numRead; char buf[BUF_SIZE]; struct param_st theParams; struct gps_st gpsStat; int sampleRate = 4000; /* * command-line processing */ debug=opterr=0; while((opt=getopt(argc,argv,optstring)) != -1) { switch(opt) { case 's': sampleRate = atoi(optarg); break; case 'd': debug++; break; case '?': case 'h': default: fprintf(stderr, "Usage: %s\n", "gp_store TODO"); exit(-1); } } if(debug) printf("gp_qc: sampRate=%d\n", sampleRate); struct sigaction sa; // sigterm sigemptyset(&sa.sa_mask); sa.sa_flags = 0; sa.sa_handler = sig_handler; if(sigaction(SIGTERM, &sa, NULL) == -1) errExit("sched: sigaction"); /* connect to the udp socket */ int ufd; // char udp_path[128]; // snprintf(udp_path, 128, "%s.%d", UDP_SOCK_PATH, (long) getpid()) // ufd = unixBind(udp_path, SOCK_DGRAM); ufd = unixConnect(UDP_SOCK_PATH, SOCK_DGRAM); if(ufd < 0) errMsg("gp_store: udp bind"); float dt_in = 1. / (float)sampleRate; int resampleRate = 500; float fpasshi, fstophi, apasshi, astophi; int npoleshi; float f3dbhi; fpasshi = (float) resampleRate / (float) sampleRate; fstophi = 1.2 * fpasshi; apasshi = 0.95; astophi = 0.05; if(debug) printf("f = %f %f a =%f %f\n", fpasshi, fstophi, apasshi, astophi); bfdesign(fpasshi, apasshi, fstophi, astophi, &npoleshi, &f3dbhi); if(debug) printf("npoles = %d f3db = %f\n", npoleshi, f3dbhi); int n=sampleRate / 10; // operate on 1 s at a time float *p, *q; if((p=(float *)malloc(n*sizeof(float))) == NULL) errExit("qc: malloc"); if((q=(float *)malloc(n*sizeof(float))) == NULL) errExit("qc: malloc"); int dec = sampleRate / resampleRate; int nDec = n / dec; float *r, *t, dt_out = dt_in * dec; if((r=(float *)malloc(nDec*sizeof(float))) == NULL) errExit("qc: malloc"); if((t=(float *)malloc(nDec*sizeof(float))) == NULL) errExit("qc: malloc"); for(i=0; i<nDec; i++) t[i] = i * dt_out; if(debug) printf("samp %d resamp %d dt %f dtout %f n %d nDec %d\n", sampleRate, resampleRate, dt_in, dt_out, n, nDec); /* allocate the space for the udppkt. The zero length array is allocated here */ struct udppkt_st *u; int nWrt, pktSize = sizeof(struct udppkt_st) + nDec * sizeof(float); if((u=malloc(pktSize)) == NULL) errExit("qc: malloc"); while(STOP == FALSE) { /* n samples are collected from DMA here... */ /* copy into p */ /* antialias filter */ if(dec > 1) { bflowpass(npoleshi, f3dbhi, n, p, q); ints8r(n, dt_in, 0., q, 0., 0., nDec, t, r); if(ufd>0) { // send the data to base station u->dt = dt_out; u->ns = nDec; // u.t0.tv_sec = ; u.t0.tv_usec = memcpy(u->d, r, nDec * sizeof(float)); if((nWrt = write(ufd, u, pktSize)) != pktSize) errMsg("qc: write"); } } } }