// Design (root-)Nyquist filter from prototype // _type : filter type (e.g. LIQUID_FIRFILT_RRRC) // _k : samples/symbol // _m : symbol delay // _beta : excess bandwidth factor, _beta in [0,1] // _dt : fractional sample delay // _h : output coefficient buffer (length: 2*k*m+1) void liquid_firdes_prototype(liquid_firfilt_type _type, unsigned int _k, unsigned int _m, float _beta, float _dt, float * _h) { // compute filter parameters unsigned int h_len = 2*_k*_m + 1; // length float fc = 0.5f / (float)_k; // cut-off frequency float df = _beta / (float)_k; // transition bandwidth float As = estimate_req_filter_As(df,h_len); // stop-band attenuation // Parks-McClellan algorithm parameters float bands[6] = { 0.0f, fc-0.5f*df, fc, fc, fc+0.5f*df, 0.5f}; float des[3] = { (float)_k, 0.5f*_k, 0.0f }; float weights[3] = {1.0f, 1.0f, 1.0f}; liquid_firdespm_wtype wtype[3] = { LIQUID_FIRDESPM_FLATWEIGHT, LIQUID_FIRDESPM_FLATWEIGHT, LIQUID_FIRDESPM_FLATWEIGHT}; switch (_type) { // Nyquist filter prototypes case LIQUID_FIRFILT_KAISER: liquid_firdes_kaiser(h_len, fc, As, _dt, _h); break; case LIQUID_FIRFILT_PM: // WARNING: input timing offset is ignored here firdespm_run(h_len, 3, bands, des, weights, wtype, LIQUID_FIRDESPM_BANDPASS, _h); break; case LIQUID_FIRFILT_RCOS: liquid_firdes_rcos(_k, _m, _beta, _dt, _h); break; case LIQUID_FIRFILT_FEXP: liquid_firdes_fexp(_k, _m, _beta, _dt, _h); break; case LIQUID_FIRFILT_FSECH: liquid_firdes_fsech(_k, _m, _beta, _dt, _h); break; case LIQUID_FIRFILT_FARCSECH: liquid_firdes_farcsech(_k, _m, _beta, _dt, _h); break; // root-Nyquist filter prototypes case LIQUID_FIRFILT_ARKAISER: liquid_firdes_arkaiser(_k, _m, _beta, _dt, _h); break; case LIQUID_FIRFILT_RKAISER: liquid_firdes_rkaiser(_k, _m, _beta, _dt, _h); break; case LIQUID_FIRFILT_RRC: liquid_firdes_rrcos(_k, _m, _beta, _dt, _h); break; case LIQUID_FIRFILT_hM3: liquid_firdes_hM3(_k, _m, _beta, _dt, _h); break; case LIQUID_FIRFILT_GMSKTX: liquid_firdes_gmsktx(_k, _m, _beta, _dt, _h); break; case LIQUID_FIRFILT_GMSKRX: liquid_firdes_gmskrx(_k, _m, _beta, _dt, _h); break; case LIQUID_FIRFILT_RFEXP: liquid_firdes_rfexp(_k, _m, _beta, _dt, _h); break; case LIQUID_FIRFILT_RFSECH: liquid_firdes_rfsech(_k, _m, _beta, _dt, _h); break; case LIQUID_FIRFILT_RFARCSECH: liquid_firdes_rfarcsech(_k, _m, _beta, _dt, _h); break; default: fprintf(stderr,"error: liquid_firdes_prototype(), invalid root-Nyquist filter type '%d'\n", _type); exit(1); } }
int main(int argc, char*argv[]) { // options unsigned int k=2; // samples/symbol unsigned int m=2; // filter delay float beta = 0.5f; // filter excess bandwidth unsigned int num_data_symbols=8; // number of data symbols int dopt; while ((dopt = getopt(argc,argv,"uhk:m:b:n:")) != EOF) { switch (dopt) { case 'u': case 'h': usage(); return 0; case 'k': k = atoi(optarg); break; case 'm': m = atoi(optarg); break; case 'b': beta = atof(optarg); break; case 'n': num_data_symbols = atoi(optarg); break; default: usage(); return 1; } } // validate options if (k < 2) { fprintf(stderr,"error: %s, interp factor must be greater than 1\n", argv[0]); return 1; } else if (m < 1) { fprintf(stderr,"error: %s, filter delay must be greater than 0\n", argv[0]); return 1; } else if (beta <= 0.0 || beta > 1.0f) { fprintf(stderr,"error: %s, beta (excess bandwidth factor) must be in (0,1]\n", argv[0]); return 1; } else if (num_data_symbols < 1) { fprintf(stderr,"error: %s, must have at least one data symbol\n", argv[0]); return 1; } // derived values unsigned int h_len = 2*k*m+1; unsigned int num_symbols = num_data_symbols + 2*m; unsigned int num_samples = k*num_symbols; // design filter and create interpolator and decimator objects float h[h_len]; // transmit filter float g[h_len]; // receive filter (reverse of h) liquid_firdes_rrcos(k,m,beta,0.3f,h); unsigned int i; for (i=0; i<h_len; i++) g[i] = h[h_len-i-1]; firinterp_crcf interp = firinterp_crcf_create(k,h,h_len); firdecim_crcf decim = firdecim_crcf_create(k,g,h_len); // allocate memory for buffers float complex x[num_symbols]; // input symbols float complex y[num_samples]; // interpolated sequence float complex z[num_symbols]; // decimated (received) symbols // generate input symbols, padded with zeros at the end for (i=0; i<num_data_symbols; i++) { x[i] = (rand() % 2 ? 1.0f : -1.0f) + (rand() % 2 ? 1.0f : -1.0f) * _Complex_I; } for ( ; i<num_symbols; i++) x[i] = 0.0f; // run interpolator for (i=0; i<num_symbols; i++) { firinterp_crcf_execute(interp, x[i], &y[k*i]); } // run decimator for (i=0; i<num_symbols; i++) { firdecim_crcf_execute(decim, &y[k*i], &z[i]); // normalize output by samples/symbol z[i] /= k; } // destroy objects firinterp_crcf_destroy(interp); firdecim_crcf_destroy(decim); // print results to screen printf("filter impulse response :\n"); for (i=0; i<h_len; i++) printf(" [%4u] : %8.4f\n", i, h[i]); printf("input symbols\n"); for (i=0; i<num_symbols; i++) { printf(" [%4u] : %8.4f + j*%8.4f", i, crealf(x[i]), cimagf(x[i])); // highlight actual data symbols if (i < num_data_symbols) printf(" *\n"); else printf("\n"); } printf("interpolator output samples:\n"); for (i=0; i<num_samples; i++) { printf(" [%4u] : %8.4f + j*%8.4f", i, crealf(y[i]), cimagf(y[i])); if ( (i >= k*m) && ((i%k)==0)) printf(" **\n"); else printf("\n"); } printf("output symbols:\n"); for (i=0; i<num_symbols; i++) { printf(" [%4u] : %8.4f + j*%8.4f", i, crealf(z[i]), cimagf(z[i])); // highlight symbols (compensate for filter delay) if ( i < 2*m ) printf("\n"); else printf(" *\n"); } // open output file FILE * fid = fopen(OUTPUT_FILENAME,"w"); fprintf(fid,"%% %s: auto-generated file\n\n", OUTPUT_FILENAME); fprintf(fid,"clear all;\n"); fprintf(fid,"close all;\n"); fprintf(fid,"k = %u;\n", k); fprintf(fid,"m = %u;\n", m); fprintf(fid,"h_len=%u;\n",h_len); fprintf(fid,"num_symbols = %u;\n", num_symbols); fprintf(fid,"num_samples = k*num_symbols;\n"); fprintf(fid,"h = zeros(1,h_len);\n"); fprintf(fid,"x = zeros(1,num_symbols);\n"); fprintf(fid,"y = zeros(1,num_samples);\n"); for (i=0; i<h_len; i++) fprintf(fid,"h(%4u) = %12.4e;\n", i+1, h[i]); for (i=0; i<num_symbols; i++) fprintf(fid,"x(%4u) = %12.4e + j*%12.4e;\n", i+1, crealf(x[i]), cimagf(x[i])); for (i=0; i<num_samples; i++) fprintf(fid,"y(%4u) = %12.4e + j*%12.4e;\n", i+1, crealf(y[i]), cimagf(y[i])); for (i=0; i<num_symbols; i++) fprintf(fid,"z(%4u) = %12.4e + j*%12.4e;\n", i+1, crealf(z[i]), cimagf(z[i])); fprintf(fid,"\n\n"); fprintf(fid,"tx = [0:(num_symbols-1)];\n"); fprintf(fid,"ty = [0:(num_samples-1)]/k - m;\n"); fprintf(fid,"tz = [0:(num_symbols-1)] - 2*m;\n"); fprintf(fid,"figure;\n"); fprintf(fid,"subplot(2,1,1);\n"); fprintf(fid," plot(ty,real(y),'-',tx,real(x),'s',tz,real(z),'x');\n"); fprintf(fid," xlabel('time');\n"); fprintf(fid," ylabel('real');\n"); fprintf(fid," grid on;\n"); fprintf(fid," legend('interp','data in','data out',0);\n"); fprintf(fid,"subplot(2,1,2);\n"); fprintf(fid," plot(ty,imag(y),'-',tx,imag(x),'s',tz,imag(z),'x');\n"); fprintf(fid," xlabel('time');\n"); fprintf(fid," ylabel('imag');\n"); fprintf(fid," grid on;\n"); fclose(fid); printf("results written to %s.\n",OUTPUT_FILENAME); printf("done.\n"); return 0; }
// create FIR polyphase filterbank channelizer object with // prototype root-Nyquist filter // _type : channelizer type (LIQUID_ANALYZER | LIQUID_SYNTHESIZER) // _M : number of channels // _m : filter delay (symbols) // _beta : filter excess bandwidth factor, in [0,1] // _ftype : filter prototype (rrcos, rkaiser, etc.) FIRPFBCH() FIRPFBCH(_create_rnyquist)(int _type, unsigned int _M, unsigned int _m, float _beta, int _ftype) { // validate input if (_type != LIQUID_ANALYZER && _type != LIQUID_SYNTHESIZER) { fprintf(stderr,"error: firpfbch_%s_create_rnyquist(), invalid type %d\n", EXTENSION_FULL, _type); exit(1); } else if (_M == 0) { fprintf(stderr,"error: firpfbch_%s_create_rnyquist(), number of channels must be greater than 0\n", EXTENSION_FULL); exit(1); } else if (_m == 0) { fprintf(stderr,"error: firpfbch_%s_create_rnyquist(), invalid filter size (must be greater than 0)\n", EXTENSION_FULL); exit(1); } // design filter unsigned int h_len = 2*_M*_m + 1; float h[h_len]; // TODO : actually design based on requested filter prototype switch (_ftype) { case LIQUID_FIRFILT_ARKAISER: // root-Nyquist Kaiser (approximate optimum) liquid_firdes_arkaiser(_M, _m, _beta, 0.0f, h); break; case LIQUID_FIRFILT_RKAISER: // root-Nyquist Kaiser (true optimum) liquid_firdes_rkaiser(_M, _m, _beta, 0.0f, h); break; case LIQUID_FIRFILT_RRC: // root raised-cosine liquid_firdes_rrcos(_M, _m, _beta, 0.0f, h); break; case LIQUID_FIRFILT_hM3: // harris-Moerder-3 filter liquid_firdes_hM3(_M, _m, _beta, 0.0f, h); break; default: fprintf(stderr,"error: firpfbch_%s_create_rnyquist(), unknown/invalid prototype (%d)\n", EXTENSION_FULL, _ftype); exit(1); } // copy coefficients to type-specfic array, reversing order if // channelizer is an analyzer, matched filter: g(-t) unsigned int g_len = 2*_M*_m; TC gc[g_len]; unsigned int i; if (_type == LIQUID_SYNTHESIZER) { for (i=0; i<g_len; i++) gc[i] = h[i]; } else { for (i=0; i<g_len; i++) gc[i] = h[g_len-i-1]; } // create filterbank object unsigned int p = 2*_m; FIRPFBCH() q = FIRPFBCH(_create)(_type, _M, p, gc); // return filterbank object return q; }