Exemple #1
0
void testFrequensyIntFlt()
{
	FreqF frf_flt;
	int i = 0;
	__int64 index=0;
	int sample=0;
	ComplexF ampl;
	int fifobuf[BUFSIZE_HI];
	Fifo_int signalBuf;
	float amplABS, amplAngle;

	signalBuf.size = BUFSIZE_HI;
	signalBuf.buf = fifobuf;
	fifo_initialize_int(&signalBuf);

	frf_flt = FreqFInit();

	//#pragma warning(disable : 4996)
	//file = fopen("freq.txt", "w");
	//sinFile = fopen("sin.txt", "w");
	//forierFreqFile = fopen("forierFreqFile.txt", "w");

	for ( index = 0; index < DEV_SampleFreq; index++)
	{
		if(index>95000)
			sample=0;
		//fprintf(sinFile, "%f; ", index * 1.0/DEV_SampleFreq);
		sample = signal(index * 1.0/DEV_SampleFreq);
		fifo_int_push(&signalBuf, sample);
		// sample = signal(index * 1/1600.0);
		periodCalcF_flt(sample, &frf_flt);

		
		ampl = fourier1st32sFreq(&signalBuf, frf_flt.T);
		amplABS = complex_abs(&ampl);
		amplAngle = complex_angle(&ampl);

		if (frf_flt.isCalc)
		{
			//signalShiftF( curFreqBuf, currentF, 20);
			frf_flt.isCalc = false;
			//fprintf(file, "%f; %f; %f; %f; %f", index * 1.0/DEV_SampleFreq, currentF, frf_flt.frequency, frf_flt.dfdt, curFreqBuf[17] - frf_flt.frequency);
				//	fprintf(file, "\r");
		}

		//fprintf(sinFile, "\r", index * 1.0/DEV_SampleFreq);
		//fprintf(forierFreqFile, "%i; %f; %f \r", sample, amplABS, amplAngle);
	}

	//fclose(file);
	//fclose(sinFile);
	//fclose(forierFreqFile);
}
// When a root is found this function is called.
// It looks in a vector to find if the given root
// was already found, if yes it returns the position
// in the vector of the root, if not it simple adds
// the root in the first avaliable position.
int process_root(_complex z, _roots *p, double eps){
    int i;
    for(i=0;i<p->nor;i++){
        if(complex_abs(complex_sub(z,p->root[i]))<2*eps){
            return i+1;
        }
    }

    p->root[p->nor]=z;
    p->nor+=1;

    return p->nor;
}
int main(){
    int     ix,iy,radius,i,tries,nit;
    double	zx,zy,zxn,zyn,cx,cy,theta,
            x,y,eps,
            poly[MAX_ROOTS+1];
    _roots roots;
    _complex z,w,fz,dfz;

    root_init(&roots);

    // Number of iteration for each pointer.
    // The bigger, the less prone to error the program will be.
    nit=100;

    // The radius where the points will be looked for.
    radius=6;

    // Precision for the roots
    eps=1E-10;

    scanf("%d",&roots.grad);

    for(i=roots.grad;i>=0;i--){
        scanf("%lf",&poly[i]);
    }

    printf("Coefficients:\n");
    for(i=roots.grad;i>=0;i--){
        printf(" a%d=%+.2f\n",i,poly[i]);
    }

    printf("\n f(0+0i)=");
    complex_print(f(complex_init(0,0),roots.grad, poly));
    printf("df(0+0i)=");
    complex_print(df(complex_init(0,0),roots.grad, poly));

    tries=0;

    do{
        tries++;
        theta=drand48()*2*M_PI;

        x=radius*cos(theta);
        y=radius*sin(theta);

        z=complex_init(x,y);
        for(i=0;i<=nit;i++){
            fz = f(z,roots.grad,poly);
            dfz=df(z,roots.grad,poly);
            if(complex_abs(dfz)<ee){
                break;
            }
            w=z;
            z=complex_sub(z,complex_div(fz,dfz));
            if(complex_abs(complex_sub(z,w))<=eps){
                process_root(z,&roots,eps);
                break;
            }
        }
    }while(roots.nor<roots.grad);

    printf("\nTook %d tries to get all %d roots\n",tries,roots.grad);

    printf("\nZeroes and their images:\n\n");
    for(i=0;i<roots.grad;i++){
        printf("Root Z%d=%+lf %+lfi \tf(z%d)=",i+1,roots.root[i].x,roots.root[i].y,i+1);
            complex_print(f(roots.root[i],roots.grad, poly));
    }

    return EXIT_SUCCESS;
}
Exemple #4
0
PHASH_EXPORT
int audiohash(float *buf, uint32_t **phash, double ***coeffs, uint8_t ***bit_toggles,\
              unsigned int *nbcoeffs, unsigned int *nbframes, double *minB, double *maxB,\
              unsigned int buflen, unsigned int P, int sr, AudioHashStInfo *hash_st){

  unsigned int i, j;

  if (buf == NULL || phash == NULL || nbframes == NULL || hash_st == NULL) return -1;

  if (sr != hash_st->sr){ 
    const double maxfreq = 3000;

    unsigned int ideal_fl = (unsigned int)(0.4*sr);
    int msbpos = 0;
    while (ideal_fl >> msbpos++);
    unsigned int upper_fl = 1 << (msbpos-1);
    unsigned int lower_fl = 1 << (msbpos-2);
    hash_st->framelength = (upper_fl - lower_fl) < (ideal_fl - lower_fl) ? upper_fl :lower_fl;

    hash_st->sr = sr;
    hash_st->window = (double*)malloc((hash_st->framelength)*sizeof(double));
    if (hash_st->window == NULL) return -1;

    for (i = 0;i<hash_st->framelength;i++){
      /*hamming window*/
      hash_st->window[i] = 0.54 - 0.46*cos(2*PI*i/(hash_st->framelength-1));
    }

    unsigned int nfft_half = hash_st->framelength/2;
    double *binbarks = (double*)malloc(nfft_half*sizeof(double));
    if (binbarks == NULL) return -1;

    double temp;
    for (i=0; i < nfft_half;i++){
      temp = i*maxfreq/nfft_half/600.0;
      binbarks[i] = 6*log(temp + sqrt(temp*temp + 1.0));
    }
    
    double lof, hif;
    hash_st->wts = (double**)malloc(nfilts*sizeof(double*));
    for (i=0;i < nfilts;i++){
      hash_st->wts[i] = (double*)malloc(nfft_half*sizeof(double));
      /*calculate wts for each filter */
      double f_bark_mid = barkfreqs[i]/600.0;
      f_bark_mid = 6*log(f_bark_mid + sqrt(f_bark_mid*f_bark_mid + 1.0));
      for (j=0;j < nfft_half ;j++){
	double barkdiff = binbarks[j] - f_bark_mid;
	lof = -2.5*(barkdiff/barkwidth - 0.5);
	hif = barkdiff/barkwidth + 0.5;
	double m = lof < hif ? lof : hif;
	m = (m < 0) ? m : 0; 
	m = pow(10,m);
	hash_st->wts[i][j] = m;
      }
    } 
    free(binbarks);
  }

  unsigned int N = buflen; 
  unsigned int framelength = hash_st->framelength;
  unsigned int nfft = framelength;
  unsigned int nfft_half = (hash_st->framelength)/2;
  unsigned int start = 0;
  unsigned int end = start + hash_st->framelength - 1;
  unsigned int overlap = 31*(hash_st->framelength)/32;
  unsigned int advance = hash_st->framelength - overlap;

  *nbframes = floor(N/advance) - floor(framelength/advance) + 1;
  
  double *window = hash_st->window;
  double **wts = hash_st->wts;

  double *frame = (double*)malloc((framelength)*sizeof(double));
  Complex *pF = (Complex*)malloc(sizeof(Complex)*(nfft));
  double *magnF = (double*)malloc((nfft_half)*sizeof(double));


  if (coeffs && nbcoeffs) *nbcoeffs = nfilts;

  double *barkdiffs = (double*)malloc((nfilts-1)*sizeof(double));
  uint8_t *tmptoggles = (uint8_t*)malloc((nfilts-1)*sizeof(uint8_t));
  if (P > 0 && bit_toggles){
    *bit_toggles = (uint8_t**)malloc((*nbframes)*sizeof(uint8_t*));
    for (j=0;j < *nbframes;j++){
      (*bit_toggles)[j] = (uint8_t*)malloc(P*sizeof(uint8_t));
    }
  }

  double **barkcoeffs = (double**)malloc((*nbframes)*sizeof(double*));
  for (i=0;i < *nbframes;i++){
    barkcoeffs[i] = (double*)malloc((nfilts)*sizeof(double));
  }
  if (coeffs && nbcoeffs) *coeffs = barkcoeffs;

   int index = 0;
   double max_bark = 0.0;
   double min_bark = 100000000.0;
   double maxF = 0.0;
   while (end < N){
       maxF = 0.0;
       for (i = 0;i<framelength;i++){
           float t1 = buf[start + i];
           double t2 = window[i];
           double t3 = frame[i];
           //NSLog("%f %f %f", t1, t2, t3);
	   frame[i] = window[i]*buf[start+i];
       }
       if (fft(frame, framelength, pF) < 0){
	   return -1;
       }
       for (i=0; i < nfft_half;i++){
           magnF[i] = complex_abs(pF[i]);
	   if (magnF[i] > maxF){
	       maxF = magnF[i];
	   }
       }

       for (i=0;i<nfilts;i++){

	   barkcoeffs[index][i] = 0.0;
	   for (j=0;j < nfft_half;j++){
	       barkcoeffs[index][i] += wts[i][j]*magnF[j];
	   }
           if (barkcoeffs[index][i] > max_bark){
	       max_bark = barkcoeffs[index][i];
	   }
	   if (barkcoeffs[index][i] < min_bark){
	       min_bark = barkcoeffs[index][i];
	   }
       }
       index += 1;
       start += advance;
       end   += advance;
   }

   if (minB) *minB = min_bark;
   if (maxB) *maxB = max_bark;

    uint32_t *hash = (uint32_t*)malloc((*nbframes)*sizeof(uint32_t));
    *phash = hash;
    for (i=0;i < *nbframes;i++){
	uint32_t hashtmp = 0;
	unsigned int m;

	for (m=0;m < nfilts-1;m++){
           double H;

	   if (i > 0){
	       H = barkcoeffs[i][m] - barkcoeffs[i][m+1] - \
                                      (barkcoeffs[i-1][m] - barkcoeffs[i-1][m+1]);
	   } else {
	       H = barkcoeffs[i][m] - barkcoeffs[i][m+1];
	   }
	   barkdiffs[m] = H;
	   tmptoggles[m] = m;

	   hashtmp <<= 1;
	   if (H > 0){
	       hashtmp |= 0x00000001;
	   }
	}

        if (bit_toggles){
	    sort_barkdiffs(barkdiffs,tmptoggles,nfilts-1);
	    for (m=0;m<P;m++){
		(*bit_toggles)[i][m] = tmptoggles[m];
	    }
	}
	hash[i] = hashtmp;
   }

    if (coeffs == NULL){
	for (i=0;i < *nbframes; i++){
	    free(barkcoeffs[i]);
	}
	free(barkcoeffs);
    }

    free(tmptoggles);
    free(barkdiffs);
    free(frame);
    free(magnF);
    free(pF);

    return 0;
}
void photic_processing(float fre_step, float HighFre, amekaData<PrimaryDataType>* PrimaryData, amekaData<SecondaryDataType>* SecondaryData)
{
	int nfft;
	nfft = (float)SAMPLE_RATE/fre_step;
	float NC = (float)nfft/2.0 + 1.0;

	PrimaryDataType* output = PrimaryData->checkPopData(nfft);
	uint16_t size =  PrimaryData->rLen;
		
	if (size > 0 && output != NULL)
	{
		int dataLen = PrimaryData->dataLen;
		PrimaryData->LRPos = (PrimaryData->LRPos + dataLen - size + 100) % dataLen;
		int isinverse = 0;
		kiss_fft_cfg st;
		kiss_fft_cpx * buf[MONTAGE_NUM];
		kiss_fft_cpx * bufout[MONTAGE_NUM];

		for (int i=0; i<MONTAGE_NUM; i++)
		{
			buf[i] = (kiss_fft_cpx*)malloc(sizeof(kiss_fft_cpx) * nfft );
			bufout[i] = (kiss_fft_cpx*)malloc(sizeof(kiss_fft_cpx) * nfft );
		}
		st = kiss_fft_alloc( nfft ,isinverse ,0,0);
			
		// Add value for input buf, then convert to frequency
		for (int j=0; j<MONTAGE_NUM; j++)
		{
			for (int i=0; i<nfft; i++)
			{
				buf[j][i].r = output[i].value[j];
				buf[j][i].i = 0;
			}
			kiss_fft( st , buf[j] ,bufout[j]);
			convert_to_freq(bufout[j], nfft);
			complex_abs(bufout[j], NC);
		}

		// Print output to file
		for (int i=0; i<(int)NC; i++)
		{
			SecondaryDataType temp;
			float fre = i * fre_step;
			temp.fre = fre;
			for (int j=0; j<MONTAGE_NUM; j++)	
			{
				if (fre < HighFre)
				{
					temp.value[j] = 0;
				}
				else
				{
					temp.value[j] = bufout[j][i].r;
				}
			SecondaryData->pushData(temp);
			}
		}
		free(st);
		for (int i=0; i<MONTAGE_NUM; i++)
		{
			free(buf[i]);
			free(bufout[i]);
		}
		delete [] output;
	}
}
Exemple #6
0
static int complex_cmp( cmplx a, cmplx b )
{
    return basic_cmp( complex_abs(a), complex_abs(b) );
}