Beispiel #1
0
float hann_response(float delta)
{
if(fabs(delta-1.0)<0.5) {
	return(gsl_sf_sinc(delta-1.0)/(delta*(1+delta)));
	}
if(fabs(delta+1.0)<0.5) {
	return(gsl_sf_sinc(delta+1.0)/(delta*(delta-1)));
	}
if(fabs(delta)<0.5) {
	return(gsl_sf_sinc(delta)/(1-delta*delta));
	}
return(sin(M_PI*delta)/(M_PI*delta*(1-delta*delta)));
}
Beispiel #2
0
double nsl_sf_window(int i, int N, nsl_sf_window_type type) {
	double v=0.0;

	switch (type) {
	case nsl_sf_window_uniform:
		if (i >= 0 && i < N)
			v = 1.0;
		else
			v = 0.0;
		break;
	case nsl_sf_window_triangle:
		v = 1.0 - 2./N*fabs(i-(N-1)/2.);
		break;
	case nsl_sf_window_triangleII:
		v = 1.0 - 2./(N-1)*fabs(i-(N-1)/2.);
		break;
	case nsl_sf_window_triangleIII:
		v = 1.0 - 2./(N+1)*fabs(i-(N-1)/2.);
		break;
	case nsl_sf_window_welch:
		v = 1.0 - gsl_pow_2(2*(i-(N-1)/2.)/(N+1));
		break;
	case nsl_sf_window_hann:
		v = 0.5*(1. - cos(2.*M_PI*i/(N-1)));
		break;
	case nsl_sf_window_hamming:
		v = 0.54 - 0.46*cos(2.*M_PI*i/(N-1));
		break;
	case nsl_sf_window_blackman:
		v = 0.42 - 0.5*cos(2.*M_PI*i/(N-1)) + 0.08*cos(4.*M_PI*i/(N-1));
		break;
	case nsl_sf_window_nuttall:
		v = 0.355768 - 0.487396*cos(2.*M_PI*i/(N-1)) + 0.144232*cos(4.*M_PI*i/(N-1)) - 0.012604*cos(6.*M_PI*i/(N-1));
		break;
	case nsl_sf_window_blackman_nuttall:
		v = 0.3635819 - 0.4891775*cos(2.*M_PI*i/(N-1)) + 0.1365995*cos(4.*M_PI*i/(N-1)) - 0.0106411*cos(6.*M_PI*i/(N-1));
		break;
	case nsl_sf_window_blackman_harris:
		v = 0.35875 - 0.48829*cos(2.*M_PI*i/(N-1)) + 0.14128*cos(4.*M_PI*i/(N-1)) - 0.01168*cos(6.*M_PI*i/(N-1));
		break;
	case nsl_sf_window_flat_top:
		v = 1 - 1.93*cos(2.*M_PI*i/(N-1)) + 1.29*cos(4.*M_PI*i/(N-1)) - 0.388*cos(6.*M_PI*i/(N-1)) + 0.028*cos(8.*M_PI*i/(N-1));
		break;
	case nsl_sf_window_cosine:
		v = sin(M_PI*i/(N-1));  
		break;
	case nsl_sf_window_bartlett_hann:
		v = 0.62 - 0.48*fabs(i/(double)(N-1)-0.5) - 0.38*cos(2.*M_PI*i/(N-1));  
		break;
	case nsl_sf_window_lanczos:
		v = gsl_sf_sinc(2.*i/(N-1)-1.);
		break;
	}

	return v;
}
Beispiel #3
0
/* construct design matrix and rhs vector for Shaw problem */
static int
shaw_system(gsl_matrix * X, gsl_vector * y)
{
  int s = GSL_SUCCESS;
  const size_t n = X->size1;
  const size_t p = X->size2;
  const double dtheta = M_PI / (double) p;
  size_t i, j;
  gsl_vector *m = gsl_vector_alloc(p);

  /* build the design matrix */
  for (i = 0; i < n; ++i)
    {
      double si = (i + 0.5) * M_PI / n - M_PI / 2.0;
      double csi = cos(si);
      double sni = sin(si);

      for (j = 0; j < p; ++j)
        {
          double thetaj = (j + 0.5) * M_PI / p - M_PI / 2.0;
          double term1 = csi + cos(thetaj);
          double term2 = gsl_sf_sinc(sni + sin(thetaj));
          double Xij = term1 * term1 * term2 * term2 * dtheta;

          gsl_matrix_set(X, i, j, Xij);
        }
    }

  /* construct coefficient vector */
  {
    const double a1 = 2.0;
    const double a2 = 1.0;
    const double c1 = 6.0;
    const double c2 = 2.0;
    const double t1 = 0.8;
    const double t2 = -0.5;

    for (j = 0; j < p; ++j)
      {
        double tj = -M_PI / 2.0 + (j + 0.5) * dtheta;
        double mj = a1 * exp(-c1 * (tj - t1) * (tj - t1)) +
                    a2 * exp(-c2 * (tj - t2) * (tj - t2));
        gsl_vector_set(m, j, mj);
      }
  }

  /* construct rhs vector */
  gsl_blas_dgemv(CblasNoTrans, 1.0, X, m, 0.0, y);

  gsl_vector_free(m);

  return s;
}
void receiver_assign_frequency(struct ControlProgram *arg){

	struct Thread_List_Item *thread_list;
	struct ControlProgram *controlprogram;
	t_fft_index *fft_array=NULL, *detrend_fft_array=NULL, *sub_fft_array=NULL;
	int i,j,k,acount,clean;
	int ncfs, nfreq;
	int blacklist_count=0;
	int best_index=0; 
	int rx_bandwidth_khz,padded_tx_sideband_khz,padded_rx_sideband_khz; //in KHz
        int detrend_enabled=0; 
        int detrend_sideband=0; 
	/* Variables for FFT diagnostic output
	*  This is a temporary diagnostic format and is not be documented.
	*  The format of the file will change based on testing needs. 
	*  TODO: re-implement as part of general diagnostic viewer for the ROS */ 
	int f_fft=-1;
	FILE *ftest=NULL;
	char data_file[255],test_file[255],strtemp[255];
	struct timespec time_now;
	struct tm* time_struct;
	int32 temp;
	float tempf;
	double templf;

	if(verbose > 0 ) {
		fprintf(stderr, "Start Freq Assignment\n");
	}
	/* SGS: where does stderr go? 
	*  http://en.wikipedia.org/wiki/Standard_streams
        */
        if(verbose > 1) {
		fprintf(stderr, "  ASSIGN FREQ: %d %d\n",
					arg->parameters->radar-1,arg->parameters->channel-1);
		fprintf(stderr, "  FFT FREQ: %d %d\n",
					arg->clrfreqsearch.freq_start_khz,arg->clrfreqsearch.freq_end_khz);
		fprintf(stderr, "  Start Freq: %lf\n",
					arg->state->fft_array[0].freq);
	}

	/* abort if no FFT data */
	if(arg->state->fft_array==NULL) { 
		fprintf(stderr,"Error in assigning frequency function\n"); 
		pthread_exit(NULL);
	}

	/* Check to see if fft diagnostic output is requested */
	test_file[0]='\0';
	strcat(test_file,"/collect.fft");
	/* If requested setup fft diagnostic file */
	ftest=fopen(test_file, "r");
	if(ftest!=NULL){
		fclose(ftest);
		data_file[0]='\0';
		clock_gettime(CLOCK_REALTIME, &time_now);
		time_struct=gmtime(&time_now.tv_sec);
		strcat(data_file, "/data/fft_samples/");
    		// data file year
       		temp=(int)time_struct->tm_year+1900;
       		ltoa(temp, strtemp, 10);
       		strcat(data_file, strtemp);
    		// data file month
       		temp=(int)time_struct->tm_mon;
       		ltoa(temp+1,strtemp,10);
       		if(temp<10) strcat(data_file, "0");
       		strcat(data_file, strtemp);
    		// data file day
       		temp=(int)time_struct->tm_mday;
       		ltoa(temp,strtemp,10);
       		if(temp<10) strcat(data_file, "0");
       		strcat(data_file, strtemp);
    		// data file hour
       		temp=(int)time_struct->tm_hour;
       		ltoa(temp,strtemp,10);
       		if(temp<10) strcat(data_file, "0");
       		strcat(data_file, strtemp);
    		// data file tens of minutes
       		temp=(int)time_struct->tm_min;
       		temp=(int)(temp/10);
       		ltoa(temp,strtemp,10);
       		strcat(data_file, strtemp);
       		if(temp<10) strcat(data_file, "0");
    		// data file suffix
       		strcat(data_file, ".");
       		temp=arg->parameters->radar-1;
       		ltoa(temp,strtemp,10);
       		strcat(data_file, strtemp);
       		strcat(data_file, ".fft");
       		f_fft=open(data_file, O_WRONLY|O_CREAT|O_NONBLOCK|O_APPEND, S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH);
  	}

	/* Write Diagnostic header info */ 
 	if(f_fft>=0){
         	temp=(int)time_struct->tm_year+1900;
         	write(f_fft, &temp, sizeof(int32));
         	temp=(int)time_struct->tm_mon+1;
         	write(f_fft, &temp, sizeof(int32));
         	temp=(int)time_struct->tm_mday;
         	write(f_fft, &temp, sizeof(int32));
         	temp=(int)time_struct->tm_hour;
         	write(f_fft, &temp, sizeof(int32));
         	temp=(int)time_struct->tm_min;
         	write(f_fft, &temp, sizeof(int32));
         	temp=(int)time_struct->tm_sec;
         	write(f_fft, &temp, sizeof(int32));
         	temp=(int)time_now.tv_nsec;
         	write(f_fft, &temp, sizeof(int32));

         	temp=arg->parameters->tbeam;
         	write(f_fft, &temp, sizeof(int32));
         	temp=arg->parameters->tfreq;
         	write(f_fft, &temp, sizeof(int32));
         	temp=arg->parameters->radar-1;
         	write(f_fft, &temp, sizeof(int32));
         	temp=arg->parameters->channel-1;
         	write(f_fft, &temp, sizeof(int32));
         	tempf=arg->parameters->baseband_samplerate;
         	write(f_fft, &tempf, sizeof(float));

         	// CLRFreqPRM
         	temp=arg->clrfreqsearch.freq_start_khz;
         	write(f_fft, &temp, sizeof(int32));
         	temp=arg->clrfreqsearch.freq_end_khz;
         	write(f_fft, &temp, sizeof(int32));
         	tempf=arg->clrfreqsearch.rx_bandwidth_khz;
         	write(f_fft, &tempf, sizeof(float));
  	}         


  /* Note here from Jeff, the FFT is typically performed on N = 2^n samples
   * so the size of the FFT window is typically larger than the band from
   * which we are selecting frequencies from.  This is however not a garuntee.
   * Each reciever driver is responsible for handling the details of how the fft is performed
   * based on the bandwidth limitation imposed on the reciever hardware. 
   * It cannot be assumed that the reciever driver was able to return a clear frequency search
   * encompassing the requested band.  
   * 
   * For current operational purposes a receiver driver is required to send 
   * back an FFT in 1 kHz bins (see the clear frequency search function for
   * specifics). Both the gc214 and the gc31X drivers comply with this
   * implicit assumption */

  /* malloc and copy data into FFT array */
  fft_array = (t_fft_index *) malloc(sizeof(t_fft_index) * arg->state->N);
  memcpy(fft_array,arg->state->fft_array,arg->state->N*sizeof(t_fft_index));

  /* the padded sidebands account for the finite bandwidths associated with
   * transmitters and receivers for a requested pulse sequence.  
   * Minimum sideband is currently set to 0 kHz.
   * These values will be used for two things:
   * 1) noise calculation for a pulse sequence relavent bandwidth 	
   * 2) impose adequate spacing for frequency assignment in multiple controlprogram 
   *     operation scenarios to avoid cross-contamination of signals

   * Note that it is padded_rx_sideband which is most critical as it is anticipated
   * that receiver bandwidth can be higher than transmit when using
   * oversampling modes. 
   * There is room for improvement here...
   * TODO: 1.) include a parameter which allows a controlprogram to explicitly
   *           override the fft 
   *       2.) smoothing separate from the sideband use for blacklisting an
   *           assigned frequency window
   */ 
  rx_bandwidth_khz=arg->clrfreqsearch.rx_bandwidth_khz;
  /* padded_tx_sideband defined but unused currently */
  padded_tx_sideband_khz = MAX(2*ceil(rx_bandwidth_khz),0);
  /* TODO: optimize padded_rx_sideband  to account for over sampling and wide-band modes*/
  padded_rx_sideband_khz = MAX(2*ceil(rx_bandwidth_khz),0);

  if (verbose > 1) {
    fprintf(stderr,"  Padded Rx Side Band: %d [kHz]\n",padded_rx_sideband_khz);
    fprintf(stderr, "  %d frequencies in fft \n",arg->state->N);
    fprintf(stderr, "  freq[%8d]: %8.3lf\n",0,(double)fft_array[0].freq);
    fprintf(stderr, "  freq[%8d]: %8.3lf\n",
            arg->state->N-1,(double)fft_array[arg->state->N-1].freq);
    fflush(stderr);
  } 
  /* JDS: If site.ini is configured to use the detrend step then use it.
   * Default to disabled if not defined in site.ini file 
   */
  detrend_enabled = iniparser_getboolean(Site_INI,
                                   "site_settings:use_clr_detrend",0);
  if (detrend_enabled) {
    if (verbose > 0) fprintf(stderr,"  Detrending Enabled\n");
    /* Get the sidebandwidth to use for detrending. Default to 50 if not set */
    detrend_sideband = iniparser_getint(Site_INI,
                                    "site_settings:detrend_sideband",50);
    /* Copy the fft array. Need to keep the detrended value to add back later
     * to get the noise power right */ 
    detrend_fft_array = (t_fft_index *)malloc(sizeof(t_fft_index)*arg->state->N);
    memcpy(detrend_fft_array,fft_array,arg->state->N*sizeof(t_fft_index));
    for (i=0; i<arg->state->N; i++){	/* run through all FFT samples */
      detrend_fft_array[i].apwr = 0;
      acount = 0;
      for (j=-detrend_sideband; j<=detrend_sideband; j++) {
        if( ((i+j) >= 0) && ((i+j) < arg->state->N) ) {
          detrend_fft_array[i].apwr += detrend_fft_array[i+j].pwr;
          acount++;
        }
      }
      detrend_fft_array[i].apwr = (double)detrend_fft_array[i].apwr/(double)acount;
      /* Save the detrended power to add back later */	
      fft_array[i].detrend=detrend_fft_array[i].apwr;
      /* Subtract off the detrended power from the original fft power */	
      fft_array[i].pwr=detrend_fft_array[i].pwr-detrend_fft_array[i].apwr;
    }
    if(detrend_fft_array!=NULL) free(detrend_fft_array);
    detrend_fft_array=NULL;
  } else {
	if (verbose > 0) fprintf(stderr,"  Detrending Disabled\n");
	for (i=0; i<arg->state->N; i++) {
		fft_array[i].detrend=0;
        }

  }
  /* Now do a less agressive narrow band smoothing over the optionally
   * detrended data */

  /* Calculate power over reciever bandwidth */
  /* Take a running average of power using 2*padded_rx_bandwith+1 number of points.*/

  /* SGS: Note here that padded_rx_sideband is specified in kHz above and
   * and the loop over the rx_sideband assumes that frequencies
   * in the FFT array are separated by 1 kHz exactly. */
  for (i=0; i<arg->state->N; i++){	/* run through all FFT samples */
    fft_array[i].apwr = 0;
    acount = 0;
    for (j=-padded_rx_sideband_khz; j<=padded_rx_sideband_khz; j++) {
      if( ((i+j) >= 0) && ((i+j) < arg->state->N) ) {
        fft_array[i].apwr += fft_array[i+j].pwr;
        templf=gsl_sf_sinc(fft_array[i+j].freq);
        acount++;
      }
    }
    if (acount > 2*padded_rx_sideband_khz) {
      fft_array[i].apwr = (double)fft_array[i].apwr/(double)acount;
    } else {
      /* Bias against selecting a frequency too near the edge of the search
       * band. Set the average pwr artificially high so that index is
       * selected against in qsort.
       */
      fft_array[i].apwr = 1E10;
    }
  }
	/* Put input fft info into diagnostic file */
  	if(f_fft>=0){
        	 //sidebands in use
        	 temp=padded_rx_sideband_khz;
        	 write(f_fft, &temp, sizeof(int32));
        	 temp=padded_tx_sideband_khz;
        	 write(f_fft, &temp, sizeof(int32));
        	 //ControlState
        	 temp=arg->state->N;
        	 write(f_fft, &temp, sizeof(int32));
        	 for(i=0;i<arg->state->N;i++) {
        	   temp=arg->state->fft_array[i].index;
        	   write(f_fft, &temp, sizeof(int32));
        	 }
        	 for(i=0;i<arg->state->N;i++) {
        	   tempf=arg->state->fft_array[i].freq;
        	   write(f_fft, &tempf, sizeof(float));
        	 }
        	 for(i=0;i<arg->state->N;i++) {
        	   tempf=arg->state->fft_array[i].pwr;
        	   write(f_fft, &tempf, sizeof(float));
        	 }
        	 for(i=0;i<arg->state->N;i++) {
        	   tempf=arg->state->fft_array[i].apwr;
        	   write(f_fft, &tempf, sizeof(float));
        	 }
  	}


	/* SGS: new logic
	 * limit the array of frequencies to the band of interest which is
	 * defined by the clear frequency search bandwidth. */

	/* note that the assumption here is that this band is an integer
	 * number of kHz and that the spacing between frequencies is 1 kHz.
	 * perhaps this a bad assumption to make... */

	ncfs = arg->clrfreqsearch.freq_end_khz - arg->clrfreqsearch.freq_start_khz;
	sub_fft_array = (t_fft_index *)malloc(sizeof(t_fft_index)*ncfs);

	j = 0;
	/* loop over all FFT frequencies and copy only those in the clrfreqsearch
	 * band to the sub array */
	for (i=0; i<arg->state->N; i++) {
		if ( (fft_array[i].freq <= arg->clrfreqsearch.freq_end_khz) &&
				(fft_array[i].freq >= arg->clrfreqsearch.freq_start_khz) ) {
			if (j >= ncfs) {	/* this should never happend, but good to check... */
				if (verbose > 0) fprintf(stderr, "Too many frequencies in clrfreqsearch band: %d\n", j);
				j = ncfs - 1;	/* make sure there is no overstepping array bounds */
			}
			/* fill the elements of the sub__fft_array from the fft_array */
			sub_fft_array[j].freq = fft_array[i].freq;
			sub_fft_array[j].pwr = fft_array[i].pwr;
			sub_fft_array[j].apwr = fft_array[i].apwr;
			sub_fft_array[j].index = fft_array[i].index;
			sub_fft_array[j].detrend = fft_array[i].detrend;
                        j++;
		}
	}

	if (verbose > 0) fprintf(stderr, "  %d frequencies in clrfreqsearch band\n", j);

	ncfs = j;	/* note that these should be the same, but perhaps should check */

	/* this should be a really high level of verbosity */
	if (verbose > 5) {
		for (i=0; i<ncfs; i++) {
			fprintf(stderr, "  %8.3g\n", sub_fft_array[i].freq);
		}
	}
	/* We now have an array of only the frequencies in the clrfreqsearch
	 * band and must now reduce the array further by eliminating frequencies
	 * that are in restricted frequency bands. */

	/* Let's resuse the fft_array instead of mallocing something else and
	 * check every band in the restricted list */
	for (i=0; i<arg->state->N; i++) {
			fft_array[i].freq=0.0;
			fft_array[i].pwr=1E10;
			fft_array[i].apwr=1E10;
			fft_array[i].index=0;
			fft_array[i].detrend=0;
        }

	/*
 	* Let's check the frequency blacklist...for science!!!!!
 	*/	

	if(blacklist!=NULL) {  
		blacklist_count=*blacklist_count_pointer;  /* Set the counter to the unmodified blacklist count */
		if (verbose>1) fprintf(stderr,"  %d %d :: Filling backlist %d\n",arg->parameters->radar,arg->parameters->channel,blacklist_count);	
		/*
		* First add all other control program's assigned frequencies to the black list   
		*/
		thread_list=controlprogram_threads;
     		while (thread_list!=NULL) {
       			controlprogram=thread_list->data;
			if(controlprogram!=NULL && arg!=NULL && controlprogram->parameters!=NULL) {                   
			  if(controlprogram!=arg) {                   
			    if(controlprogram->state!=NULL) {                   
       				if(controlprogram->state->active!=0) {
           				if (blacklist_count < max_blacklist) {  
           					/* place controlprogram's assigned frequency on the blacklist */
           					blacklist[blacklist_count].start=controlprogram->state->current_assigned_freq-controlprogram->state->rx_sideband;
           					blacklist[blacklist_count].end=controlprogram->state->current_assigned_freq+controlprogram->state->rx_sideband;
           					blacklist[blacklist_count].program=(uint64)controlprogram;
           					if (verbose> 0) 
							fprintf(stderr,"  %d %d :: Adding backlist :: %d %d :  %d %d\n", \
                       					  arg->parameters->radar,arg->parameters->channel, \
                       					  controlprogram->parameters->radar,controlprogram->parameters->channel, \
                       					  blacklist[blacklist_count].start,blacklist[blacklist_count].end);	
           					blacklist_count++;
           					if (blacklist_count >= max_blacklist) blacklist_count=max_blacklist-1; 
					}
         			//} 
				}
                            }
       			  }
			}
       			thread_list=thread_list->prev;
     		}
		if(verbose > 0 ) {
			fprintf(stderr,"  Current blacklist:\n");
			for (k=0; k<blacklist_count; k++) {
				fprintf(stderr,"  %8d %8d : %lu\n",blacklist[k].start,blacklist[k].end,(unsigned long) blacklist[k].program);
			}
		}

		/* Put the updated blacklist information into the diagnostic file */
		if(f_fft>=0){
	       		temp=blacklist_count;
	       		write(f_fft, &temp, sizeof(int32));			
			for (k=0; k<blacklist_count; k++) {
				temp=blacklist[k].start;
	       			write(f_fft, &temp, sizeof(int32));
			}
			for (k=0; k<blacklist_count; k++) {
				temp=blacklist[k].end;
	       			write(f_fft, &temp, sizeof(int32));
			}
			for (k=0; k<blacklist_count; k++) {
				temp=blacklist[k].program;
	       			write(f_fft, &temp, sizeof(int32));
			}
		}

		/* Now let's select the subset of fft frequencies not in the updated blacklist */
		j = 0;
		for (i=0; i<ncfs-1; i++) {
			clean = 1;
			for (k=0; k<blacklist_count; k++) {
				if ((sub_fft_array[i].freq <= blacklist[k].end) &&
						(sub_fft_array[i].freq >= blacklist[k].start)) {
					clean = 0;		/* frequency is in a restricted band, don't bother */
					break;
				}
			}

			if (clean) {	/* this is a good one */
			        fft_array[j].freq= sub_fft_array[i].freq;
			        fft_array[j].pwr= sub_fft_array[i].pwr;
			        fft_array[j].apwr= sub_fft_array[i].apwr;
			        fft_array[j].index= sub_fft_array[i].index;
			        fft_array[j].detrend= sub_fft_array[i].detrend;
				j++;
			}
		}

		nfreq = j;	/* these are the number of allowed frequencies */

		if (verbose > 1)
			fprintf(stderr, "  %d frequencies allowed\n", nfreq);

		/* now let's get an array that is the proper size and transfer
		 * everything to that array */
		if(sub_fft_array!=NULL) free(sub_fft_array);
                sub_fft_array=NULL; 
		sub_fft_array = (t_fft_index *)malloc(sizeof(t_fft_index)*nfreq);
	        memcpy(sub_fft_array,fft_array,nfreq*sizeof(t_fft_index));
		if(fft_array!=NULL) free(fft_array);
                fft_array=NULL; 
                		
	} else {
		/* I am assuming that if blacklist is NULL there is no restricted
		 * frequencies (which is unlikely) or there was a problem with reading
		 * the restrict.dat file */
		nfreq = ncfs;

		if(fft_array!=NULL) free(fft_array);
		fft_array=NULL;
	}
	/* The only array we now have is sub_fft_array and it contains only those
	 * frequencies that are in the clear frequency search band and not in
	 * any of the restricted frequency bands */

	/* sort the array of allowable frequencies */
	qsort(sub_fft_array, nfreq, sizeof(sub_fft_array[0]), compare_fft_structs);
        if (detrend_enabled) {
		/* Add back the detrended value to get the correct noise value */
         	for(i=0;i<nfreq;i++) {
           		sub_fft_array[i].pwr=sub_fft_array[i].pwr+sub_fft_array[i].detrend;
           		sub_fft_array[i].apwr=sub_fft_array[i].apwr+sub_fft_array[i].detrend;
		}
	}
	/* Put sorted and available frequencies into diagnostic file */
	 if(f_fft>=0){
	       	temp=nfreq;
	        write(f_fft, &temp, sizeof(int32));
         	for(i=0;i<nfreq;i++) {
           		temp=sub_fft_array[i].index;
           		write(f_fft, &temp, sizeof(int32));
         	}
         	for(i=0;i<nfreq;i++) {
           		tempf=sub_fft_array[i].freq;
           		write(f_fft, &tempf, sizeof(float));
         	}
         	for(i=0;i<nfreq;i++) {
           		tempf=sub_fft_array[i].pwr;
           		write(f_fft, &tempf, sizeof(float));
         	}
         	for(i=0;i<nfreq;i++) {
           		tempf=sub_fft_array[i].apwr;
           		write(f_fft, &tempf, sizeof(float));
         	}
  	}


        if(nfreq > 0 ) {
		/* Just here for logging purposes */
   		if (verbose>0) {	
     			fprintf(stderr,"  Highest 5 freqs: \n");
     			for(i = nfreq-1; i > nfreq-6; i--){
    				if(i >= 0) { 
          				fprintf(stderr,"  %d: freq = %8.3lf, pwr = %8.3lf apwr = %8.3lf\n",i,sub_fft_array[i].freq,sub_fft_array[i].pwr,sub_fft_array[i].apwr);
				} else {
          				fprintf(stderr,"  %d: Not enough valid frequencies in fft %d\n",i,nfreq);
				}
     			}
     			fprintf(stderr," Lowest 5 freqs: \n");
     			for(i = 0; i< 5;i++){
    				if(i < nfreq) { 
          				fprintf(stderr,"  %d: freq = %8.3lf, pwr = %8.3lf apwr = %8.3lf\n",i,sub_fft_array[i].freq,sub_fft_array[i].pwr,sub_fft_array[i].apwr);
				} else {
          				fprintf(stderr,"  %d: Not enough valid frequencies in fft %d\n",i,nfreq);
				}
     			}
   		}	
		/*
		* Now set best available frequency from the sorted sub_fft_array as this controlprogram's best assigned frequency   
		*/
  		best_index=0; 
        	arg->state->current_assigned_freq=sub_fft_array[best_index].freq;
        	arg->state->current_assigned_noise=sub_fft_array[best_index].apwr*arg->clrfreqsearch.rx_bandwidth_khz;

  		if(verbose > 0 ) fprintf(stderr,"  current assigned frequency: %d\n", arg->state->current_assigned_freq);
  		if(verbose > 0 ) fprintf(stderr,"  current assigned noise: %lf\n", arg->state->current_assigned_noise);

		arg->state->tx_sideband=padded_tx_sideband_khz;
		arg->state->rx_sideband=padded_rx_sideband_khz;
		/*
		* TODO: check to see if any controlprogram has a currently assigned frequency that conflicts with the best freq   
		* priority :high number wins
		* Kick lower priority control programs to give up their frequency.
		*/

	} else {
		/* JDS QUESTION: Is this the best fallback when no valid frequency is available?
 		*  A simple single _default_ for all controlprograms is not valid in a multi channel configuration */ 
   		if (verbose>0) fprintf(stderr,"No valid frequencies Setting best available frequency to zero with very high noise\n");
        	arg->state->current_assigned_freq=0;
        	arg->state->current_assigned_noise=1E10;

	}
	/* Put assignment data into diagnostic file */
	if(f_fft>=0){
	       temp=arg->state->current_assigned_freq;
	       write(f_fft, &temp, sizeof(int32));
	       tempf=arg->state->current_assigned_noise;
	       write(f_fft, &tempf, sizeof(float));
	       close(f_fft);
	}

/*
 * Final Clean up
 */
	if(sub_fft_array!=NULL) free(sub_fft_array);
	sub_fft_array=NULL;
        if (verbose>-0) fprintf(stderr,"Exiting assignment\n");
	pthread_exit(NULL);
}
Beispiel #5
0
int nsl_sf_apply_window(double data[], size_t N, nsl_sf_window_type type) {
	if (N == 0)
		return -1;

	size_t i;
	switch (type) {
	case nsl_sf_window_uniform:
		/* do nothing */
		break;
	case nsl_sf_window_triangle:
		for (i = 0; i < N; i++)
			data[i] = 1.0 - 2./N*fabs(i-(N-1)/2.);
		break;
	case nsl_sf_window_triangleII:
		for (i = 0; i < N; i++)
			data[i] = 1.0 - 2./(N-1)*fabs(i-(N-1)/2.);
		break;
	case nsl_sf_window_triangleIII:
		for (i = 0; i < N; i++)
			data[i] = 1.0 - 2./(N+1)*fabs(i-(N-1)/2.);
		break;
	case nsl_sf_window_welch:
		for (i = 0; i < N; i++)
			data[i] = 1.0 - gsl_pow_2(2*(i-(N-1)/2.)/(N+1));
		break;
	case nsl_sf_window_hann:
		for (i = 0; i < N; i++)
			data[i] = 0.5*(1. - cos(2.*M_PI*i/(N-1)));
		break;
	case nsl_sf_window_hamming:
		for (i = 0; i < N; i++)
			data[i] = 0.54 - 0.46*cos(2.*M_PI*i/(N-1));
		break;
	case nsl_sf_window_blackman:
		for (i = 0; i < N; i++)
			data[i] = 0.42 - 0.5*cos(2.*M_PI*i/(N-1)) + 0.08*cos(4.*M_PI*i/(N-1));
		break;
	case nsl_sf_window_nuttall:
		for (i = 0; i < N; i++)
			data[i] = 0.355768 - 0.487396*cos(2.*M_PI*i/(N-1)) + 0.144232*cos(4.*M_PI*i/(N-1)) - 0.012604*cos(6.*M_PI*i/(N-1));
		break;
	case nsl_sf_window_blackman_nuttall:
		for (i = 0; i < N; i++)
			data[i] = 0.3635819 - 0.4891775*cos(2.*M_PI*i/(N-1)) + 0.1365995*cos(4.*M_PI*i/(N-1)) - 0.0106411*cos(6.*M_PI*i/(N-1));
		break;
	case nsl_sf_window_blackman_harris:
		for (i = 0; i < N; i++)
			data[i] = 0.35875 - 0.48829*cos(2.*M_PI*i/(N-1)) + 0.14128*cos(4.*M_PI*i/(N-1)) - 0.01168*cos(6.*M_PI*i/(N-1));
		break;
	case nsl_sf_window_flat_top:
		for (i = 0; i < N; i++)
			data[i] = 1 - 1.93*cos(2.*M_PI*i/(N-1)) + 1.29*cos(4.*M_PI*i/(N-1)) - 0.388*cos(6.*M_PI*i/(N-1)) + 0.028*cos(8.*M_PI*i/(N-1));
		break;
	case nsl_sf_window_cosine:
		for (i = 0; i < N; i++)
			data[i] = sin(M_PI*i/(N-1));  
		break;
	case nsl_sf_window_bartlett_hann:
		for (i = 0; i < N; i++)
			data[i] = 0.62 - 0.48*fabs(i/(double)(N-1)-0.5) - 0.38*cos(2.*M_PI*i/(N-1));  
		break;
	case nsl_sf_window_lanczos:
		for (i = 0; i < N; i++)
			data[i] = gsl_sf_sinc(2.*i/(N-1)-1.);
		break;
	}

	return 0;
}