コード例 #1
0
ファイル: test_peakfind.c プロジェクト: karchie/peakfind
int main(int argc, const char *argv[]) {
  const int nx = 32, ny = 32, nz = 32, dim = nx*ny*nz;
  const int dims[3] = {nx, ny, nz};
  const float mmppixr[] = { 2, 2, 2 }, centerr[] = { 0, 0, 0};
  const float blur_radius = 4.0;
  int iz, iy, ix, i;
  float *image, *mask, *roi;

  pf_log_all(1);

  image = calloc(dim, sizeof(float));
  roi = calloc(dim, sizeof(float));
  mask = malloc(dim * sizeof(float));

  if (!image || !mask || !roi) {
    fprintf(stderr, "allocation failed");
    exit(1);
  }

  /* start with a trivial mask and a gaussian image*/
  float min = INT_MAX, max = INT_MIN;
  for (i = iz = 0; iz < nz; iz++) {
    for (iy = 0; iy < ny; iy++) {
      for (ix = 0; ix < nx; ix++, i++) {
        mask[i] = 1.0;
        image[i] = gauss3d(ix-(nx/2),iy-(ny/2),iz-(ny/2),nx/4);
        if (image[i] > max) {
          max = image[i];
        }
        if (image[i] < min) {
          min = image[i];
        }
      }
    }
  }
  fprintf(stdout, "image value range: [%g, %g]\n", min, max);
    

  display(image, dims, 17, max);

  sphereblur(image, dims, mmppixr, blur_radius);

  display(image, dims, 17, max);

  find_peaks(image, dims, mmppixr, centerr,
             0, 0, 0, 0, 1,
             roi, 4, 0, 8, mask, 0, 0);

  puts("slice at peak:\n");
  display(roi, dims, 17, max);
  puts("2 slices away from peak:\n");
  display(roi, dims, 15, max);
  puts("4 slices away from peak:\n");
  display(roi, dims, 13, max);
}
コード例 #2
0
void FormOptimization::update_plots() {

  bool have_data = false;
  bool have_peaks = false;

  for (auto &q: current_spectra_.by_type("1D")) {
    Qpx::Spectrum::Metadata md;
    if (q)
      md = q->metadata();

    if (md.total_count > 0) {
      have_data = true;
      fitter_opt_.setData(q);
    }
  }

  if (have_data) {
    have_peaks = find_peaks();
    int current_spec = spectra_.size() - 1;
    spectra_[current_spec] = fitter_opt_;
    setting_fwhm_[current_spec] = peaks_[current_spec].fwhm_hyp;

    ui->tableResults->setRowCount(peaks_.size());

    QTableWidgetItem *st = new QTableWidgetItem(QString::number(setting_values_[current_spec]));
      st->setFlags(st->flags() ^ Qt::ItemIsEditable);
      ui->tableResults->setItem(current_spec, 0, st);

      QTableWidgetItem *en = new QTableWidgetItem(QString::number(peaks_[current_spec].energy));
      en->setFlags(en->flags() ^ Qt::ItemIsEditable);
      ui->tableResults->setItem(current_spec, 1, en);

      QTableWidgetItem *fw = new QTableWidgetItem(QString::number(peaks_[current_spec].fwhm_hyp));
      fw->setFlags(fw->flags() ^ Qt::ItemIsEditable);
      ui->tableResults->setItem(current_spec, 2, fw);

      QTableWidgetItem *area = new QTableWidgetItem(QString::number(peaks_[current_spec].area_best.val));
      area->setFlags(area->flags() ^ Qt::ItemIsEditable);
      ui->tableResults->setItem(current_spec, 3, area);

      QTableWidgetItem *err = new QTableWidgetItem(QString::number(peaks_[current_spec].sum4_.peak_area.err()));
      err->setFlags(err->flags() ^ Qt::ItemIsEditable);
      ui->tableResults->setItem(current_spec, 4, err);
  }

  if (ui->plotSpectrum->isVisible())
    ui->plotSpectrum->update_spectrum();

  if (have_peaks && !peaks_.empty() && (peaks_.back().sum4_.peak_area.err() < ui->doubleError->value()))
    interruptor_.store(true);

  resultChosen();
}
コード例 #3
0
ファイル: icpc2015A.c プロジェクト: jamiechapmanbrn/icpc2015A
// Finds the largest price decline in the price of artichokes
// This function uses the min/max method
double findLargestDecline( int n, coefficient_struct equation )
{
	pointHolder zeros;



	// These are zeros of the derivitive, tehy will be the local maxima/minima of interest
	zeros = find_peaks( equation , n );

	return( findDeclineFromPoints( zeros ) );


}
コード例 #4
0
sample PeakODF::process_frame(int signal_size, sample* signal)
{
    PeakList* peaks = track_peaks(find_peaks(frame_size, &signal[0], mq_params),
                                  mq_params);

    /* calculate the amplitude differences between bins from consecutive frames */
    sample sum = 0.0;
    while(peaks && peaks->peak)
    {
        sum += get_distance(peaks->peak, peaks->peak->prev);
        peaks = peaks->next;
    }
    return sum;
}
コード例 #5
0
void RV_Missing_t_walk_core::find_slice() {
    total_slice_length = 0.0;
    intervals.clear();
    find_peaks();

    peaks.erase(
        std::remove_if(peaks.begin(), peaks.end(),
            [=](double x) {
                return lpdf(x) < ly ? true : false;
            }
        ),
        peaks.end()
    );
    if (peaks.size() < 1) {
			std::cout << "Warning: peak finding failed, defined from" 
									 "\non current state." << std::endl;
			peaks.push_back(x2);
		}
//        throw std::runtime_error("No peaks found above slice.");

    // step out from peak.
    peak_bound_lr.clear();
    for (std::vector<double>::iterator i = peaks.begin();
                i != peaks.end(); ++i) {
        peak_bound_lr.push_back(step_out(i));
    }
    // Fix overlap
    for (unsigned int i = 0; i != (peak_bound_lr.size()-1); i++) {
        if (peak_bound_lr[i+1][0] < peak_bound_lr[i][1] ) {
            double mid = (peaks[i+1] + peaks[i])/2.0;
            peak_bound_lr[i+1][0] = mid;
            peak_bound_lr[i][1] = mid;
        }
    }

    for (std::vector<std::vector<double> >::iterator i = peak_bound_lr.begin();
                i != peak_bound_lr.end(); ++i)
    {
        bool lb = (i == (peak_bound_lr.end() - 1 ));
        // Calculate sub-slice to sub-slice distances:
        total_slice_length += (*i)[1] - (*i)[0];
        if (!lb) {
            intervals.push_back((*(i+1))[0] - (*i)[1]);
        }
    }
    if (peak_bound_lr.size() != peaks.size() )
        throw std::runtime_error("Mismatch between number of peaks and number of bounds.");

}
コード例 #6
0
ファイル: dcaenc.c プロジェクト: 0day-ci/FFmpeg
static int encode_frame(AVCodecContext *avctx, AVPacket *avpkt,
                        const AVFrame *frame, int *got_packet_ptr)
{
    DCAEncContext *c = avctx->priv_data;
    const int32_t *samples;
    int ret, i;

    if ((ret = ff_alloc_packet2(avctx, avpkt, c->frame_size, 0)) < 0)
        return ret;

    samples = (const int32_t *)frame->data[0];

    subband_transform(c, samples);
    if (c->lfe_channel)
        lfe_downsample(c, samples);

    calc_masking(c, samples);
    find_peaks(c);
    assign_bits(c);
    calc_scales(c);
    quantize_all(c);
    shift_history(c, samples);

    init_put_bits(&c->pb, avpkt->data, avpkt->size);
    put_frame_header(c);
    put_primary_audio_header(c);
    for (i = 0; i < SUBFRAMES; i++)
        put_subframe(c, i);


    for (i = put_bits_count(&c->pb); i < 8*c->frame_size; i++)
        put_bits(&c->pb, 1, 0);

    flush_put_bits(&c->pb);

    avpkt->pts      = frame->pts;
    avpkt->duration = ff_samples_to_time_base(avctx, frame->nb_samples);
    avpkt->size     = put_bits_count(&c->pb) >> 3;
    *got_packet_ptr = 1;
    return 0;
}
コード例 #7
0
ファイル: peaks.c プロジェクト: michaelklachko/Learning-C
main() {

	int r, c;
	int terra[R][C];

	//generate a text file with data:
	FILE *terrain;
	terrain = fopen("terrain.txt", "w");

	for(r=0; r<R; r++) {

		for(c=0; c<C; c++) 

			fprintf(terrain, "%2i ", rand()%100);

		fprintf(terrain, "%c", '\n');
	}

	fclose(terrain);

	//read the data from text file to an array
	terrain = fopen("terrain.txt", "r");

	if(terrain == NULL)

		printf("Can't open terrain.txt\n\n");

	else

		for(r=0; r<R; r++)

			for(c=0; c<C; c++)

				fscanf(terrain, "%i", &terra[r][c]); 
				
	
	//show the peaks
	find_peaks(terra);

}
コード例 #8
0
int main (int argc, char *argv[]) {

	char *filename_AG = NULL;
	char *filename_WD = NULL;
	int num_WD = 0;
	char *filename_AGWD = NULL;
	char *filename_ALN = NULL;
	float thresholdWD = 0.00;
	unsigned int p0AG = 0;
	unsigned int p0WD = 0;

	switch (argc) {
		case 2:
			if (strcmp(argv [1], "--help") == 0) {
				welcome();
				help();
				return 0;
			}
			break;
		case 17:
			if (strcmp(argv [1], "--sequence") == 0 &&
					strcmp(argv [3], "--wd") == 0 &&
					strcmp(argv [5], "--num") == 0 &&
					strcmp(argv [7], "--out") == 0 &&
					strcmp(argv [9], "--aln") == 0 &&
					strcmp(argv [11], "--th") == 0 &&
					strcmp(argv [13], "--pwd") == 0 &&
					strcmp(argv [15], "--pag") == 0) {
				filename_AG = argv[2];
				filename_WD = argv[4];
				assert(sscanf(argv [6], "%d", &num_WD) == 1);
				filename_AGWD = argv[8];
				filename_ALN = argv[10];
				assert(sscanf(argv [12], "%f", &thresholdWD) == 1);
				assert(sscanf(argv [14], "%d", &p0WD) == 1);
				assert(sscanf(argv [16], "%d", &p0AG) == 1);
			}
			else
				return -1;
			break;
		default:
			welcome();
			help();
			return 0;
			break;
	}
	/* The num_WD variable is not needed any more.
	 * I keep it just for because it helps me while 
	 * reading at the log files.
	 */
	printf("Working on WD %d\n", num_WD);

	/* Here I create the PCM objects, crucial for
	 * PCM data import.
	 * pcmWD contains the WD data (extracted from the US stream)
	 * pcmAG contains the AG sequence, as shipped by the AG500 machine from hell
	 */
	mObjectPCM<int16_t> *pcmWD;
	mObjectPCM<int16_t> *pcmAG;
	pcmWD = new mObjectPCM<int16_t>(MPCM_STEREO, MPCM_STREAM_TINY);
	pcmAG = new mObjectPCM<int16_t>(MPCM_MONO,   MPCM_STREAM_TINY);
	
	/* Read the previoulsy described WAV files */
	mAudio2PCM(filename_WD, 0, 0, pcmWD);
	mAudio2PCM(filename_AG, 0, 0, pcmAG);
	
	/* Free unused memory */
	pcmWD->Trim();
	pcmAG->Trim();

	/* Here I dump the PCM informations in handy buffers 
	 * bufferWD contains the WD data, right channel of the US stream
	 * bufferSN contains the SN (sync) data, left channel of the US stream 
	 * bufferAG contains the AG SEQ data. This buffer will be used r/w 
	 *          for peak detection. The original information is gonna
	 *          be lost. In fact...
	 * bufferAG_copy ...contains a copy of the AG SEQ data
	 */
	int16_t *bufferWD = NULL;
	int16_t *bufferSN = NULL;
	int16_t *bufferAG = NULL;
	int16_t *bufferAG_copy = NULL;

	/* A size for almost each buffer. Almost each means
	 * that bufferWD and bufferSN have the same size samplesWD.
	 * Also bufferAG and its copy bufferAG_copy have the same size,
	 * but they are used for processing, so I prefere to keep two 
	 * sizes.
	 */
	unsigned int samplesWD;
	unsigned int samplesAG;	
	unsigned int samplesAG_copy;	

	/* Fill the buffers... */
	pcmWD->ExportBuffers(&bufferSN, &bufferWD, samplesWD);
	pcmAG->ExportBuffer(&bufferAG, samplesAG);
	pcmAG->ExportBuffer(&bufferAG_copy, samplesAG_copy);
	
	/* ... and release the unused memory. */
	delete pcmAG;
	delete pcmWD;
	free(bufferSN);
	
	/* Threshold the WD and AG buffers.
	 * AG buffer thresholding uses a standard value
	 * WD buffer requires the same value used by pdetect2.
	 * The value is stored in config/lm_pdetect.cfg
	 */
#ifdef USE_SAT_INVERT
	negative(bufferWD, samplesWD);
	negative(bufferAG, samplesAG);
#endif
	threshold(bufferWD, samplesWD, thresholdWD);
	threshold(bufferAG, samplesAG, AG_TH);

#ifdef DEVELOP
	/* Now is time to look at the thresholded signals */
	mEncodePCM16("debug/th_wd.wav", bufferWD, NULL, samplesWD, 48000, 1);
	mEncodePCM16("debug/th_ag.wav", bufferAG, NULL, samplesAG, 16000, 1);
#endif
	
	/* Remove spikes and artifacts. 
	 * clean_spurious_spikes runs twice, just because I'm used 
	 * to work on the clean_spikes code. It's a preventive bug-fix.
	 */
	unsigned int peaksWD;

	clean_spurious_spikes(bufferWD, samplesWD);
	peaksWD = clean_spikes(bufferWD, samplesWD, US_DS_PEAK, US_DS_ARTI);
	clean_spurious_spikes(bufferWD, samplesWD);

	unsigned int peaksAG;
	clean_spurious_spikes(bufferAG, samplesAG);
	//peaksAG = clean_spikes(bufferAG, samplesAG, AG_DS_PEAK, AG_DS_ARTI);
	peaksAG = clean_spikes(bufferAG, samplesAG, 10, AG_DS_ARTI);
	clean_spurious_spikes(bufferAG, samplesAG);
	
	printf("Peaks found on RAW data:\n");
	printf("     WD data:  %d\n", peaksWD);
	printf("     AG data: %d\n", peaksAG);
	
#ifdef DEVELOP
	/* Now is time to look at those spiky signals */
	mEncodePCM16("debug/p_wd.wav", bufferWD, NULL, samplesWD, 48000, 1);
	mEncodePCM16("debug/p_ag.wav", bufferAG, NULL, samplesAG, 16000, 1);
#endif

	/* Sample-and-Hold AG signal (the spiky one) 
	 * This method works fine with spiky signals since
	 * it samples and holds a sample value for samplesAGR
	 * times.
	 * Q: Why 3?
	 * A: AG = 16kHz, WD = 48kHz. I want to compare the WD spiky
	 * signal with the resampled version or the AG spiky signal, find
	 * the WD peaks there, calculate the lag, crop the AG signal rescaling
	 * bach the AGR sample values.
	 */
	unsigned int samplesAGR = 3 * samplesAG;
	int16_t *bufferAGR = (int16_t *)malloc(samplesAGR * sizeof(int16_t));
	memset(bufferAGR, 0, samplesAGR * sizeof(int16_t));
	
	resample_linear(bufferAG, bufferAGR, samplesAG, 3, 0);
	
#ifdef DEVELOP
	/* Now is time to look at the spiky signal, resampled. */
	mEncodePCM16("debug/p_agr.wav", bufferAGR, NULL, samplesAGR, 48000, 1);
#endif

	/* Collect the peaks found on the AGR data */
	WD_peaks peaks_dataAGR;
	memset(&peaks_dataAGR, 0, sizeof(WD_peaks));
	find_peaks(bufferAGR, samplesAGR, peaks_dataAGR, 0);
	
	/* Collect the peaks found on the WD data */
	WD_peaks peaks_dataWD;
	memset(&peaks_dataWD, 0, sizeof(WD_peaks));
	find_peaks(bufferWD, samplesWD, peaks_dataWD, 0);
	
	printf("Peaks found on cleaned data:\n");
	printf("     WD data:  %d\n", peaks_dataWD.tot);
	printf("     AGR data: %d\n", peaks_dataAGR.tot);
	
#ifdef DEVELOP
	printf("Peaks AGR:\n");
	print_peaks(peaks_dataAGR);
	
	printf("Peaks WD:\n");
	print_peaks(peaks_dataWD);
#endif

	/* Sequence peak-consistency check.
	 * Controls if the sequence starts with 3 +SAT peaks 
	 * and if it ends with 3 -SAT peaks.
	 */
	bool start_ok = check_sequence_start(peaks_dataAGR);
	bool stop_ok  = check_sequence_stop(peaks_dataAGR);
	printf("AGR sequence peak-consistency:\n");
	printf("     Start: %s\n", start_ok ? "passed" : "failed");
	printf("     Stop:  %s\n", stop_ok ? "passed" : "failed");

	/* Two cases here:
	 * - If we are aligning using the WD start peak, it means
	 *   that the WD stop peak is/is not distorted/broken/ghosted,
	 *   so the WD start peak will be the first one in 
	 *   peaks_dataWD.
	 * - If we are aligning using the WD stop peak, it means
	 *   that the WD start peak is broken. 
	 *   Just one peak should be present in peaks_dataWD 
	 * So, p0WD_idx is alwais equal to 0!
	 */
	unsigned int p0WD_idx = 0;
	printf("Peaks used for the alignment:\n");
	printf("     AGR: %d (value=%d, lenght=%d)\n",
			p0AG, peaks_dataAGR.type[p0AG], peaks_dataAGR.length[p0AG]);
	printf("     WD:  %d (value=%d, lenght=%d)\n", 
			p0WD, peaks_dataWD.type[p0WD_idx], peaks_dataWD.length[p0WD_idx]);

	/* Let's use AGR as reference and find where the WD 
	 * word is, calculating the lag between the two signals.
	 */
	unsigned int s0AGR = peaks_dataAGR.start[p0AG] - peaks_dataWD.start[p0WD_idx];
	unsigned int s1AGR = s0AGR + samplesWD;

	/*
	if(p0WD == 0)
		assert(peaks_dataWD.tot == 2);
		*/
	if(p0WD == 1) {
		printf("Error: user supplied pwd does not match WD peak data\n");
		assert(peaks_dataWD.tot == 1);
	}

	/*
	unsigned int s0AGR = 0;
	unsigned int s1AGR = 0;
	if(p0WD == 0) {
		s0AGR = peaks_dataAGR.start[p0AG] - peaks_dataWD.start[p0WD];
		s1AGR = s0AGR + samplesWD;
	}
	else {
		s0AGR = peaks_dataAGR.start[p0AG] - peaks_dataWD.start[p0WD_idx];
		s1AGR = s0AGR + samplesWD;
	}
	*/

	/* Scale the AGR samples values back to 16kHz (for AG)... */
	unsigned int s0AG = s0AGR/3;
	unsigned int s1AG = s1AGR/3;
	/* ...and to 200Hz for AGAMP/POS. */
	unsigned int s0AGAMP = s0AG/80;
	unsigned int s1AGAMP = s1AG/80;

	/* Done! As usual, put spam on the term... */
	printf("Results, in full ALN fashion:\n");
	printf("     AGR:   %d:%d\n", s0AGR, s1AGR); 
	printf("     AG:    %d:%d\n", s0AG,  s1AG); 
	printf("     AGAMP: %d:%d\n", s0AGAMP, s1AGAMP); 

	/* I'm ready to save a cropped version of the AG SEQ file, that
	 * matches in lenght the WD word.
	 * Here I crop...
	 */
	unsigned int samplesAGWD = s1AG - s0AG + 1;
	int16_t *bufferAGWD = (int16_t *)malloc(samplesAGWD * sizeof(int16_t));

	/* .. and here I put ham on some audio file... */
	memcpy(bufferAGWD, bufferAG_copy + s0AG, samplesAGWD * sizeof(int16_t));
	mEncodePCM16(filename_AGWD, bufferAGWD, NULL, samplesAGWD, 16000, 1);

	/* .. and spam on some ALN file. */
	FILE *file_align = fopen(filename_ALN, "w");
	fprintf(file_align, "%d/%d/", s0AGR, s1AGR);
	fprintf(file_align, "%d/%d/", s0AG, s1AG);
	fprintf(file_align, "%d/%d\n", s0AGAMP, s1AGAMP);
	fclose(file_align);
	
	/* Cleaning up memory */
	free(bufferWD);
	free(bufferAG);
	free(bufferAG_copy);
	free(bufferAGR);

	return 0;
}