Ejemplo n.º 1
0
int main(int argc, char const *argv[]) {
	int n, i, *array, value, n1, n2, peaks;
	scanf("%d", &n);
	while(n != 0) {
		array = malloc(n * sizeof(int));
		peaks = 0;
		for(i = 0; i < n; i++)
			scanf("%d", array + i);
		if(n == 2) {
			peaks = 2;
			goto REPORT_ANS;
		}
		for(i = 0; i < n; i++) {
			getNeighbors(n, i, &n1, &n2);
			value = *(array + i);
			n1 = *(array + n1);
			n2 = *(array + n2);
			if(isPeak(value, n1, n2))
				peaks++;
		}
		REPORT_ANS:
			printf("%d\n", peaks);
		scanf("%d", &n);
	}
	return 0;
}
Ejemplo n.º 2
0
void Gradient::printAnglesList()
{
    for (int n = 0; isPeak(n); n++){
        cout << "x,y,t:\t" << (int)getAnglesXCoord(n)
             << "\t" << (int)getAnglesYCoord(n)
             << "\t" << (int)getAngle(n) << endl;
    }
}
void SimplePeakFinder::findPeaks(const std::vector<double>& signal, std::vector<unsigned int>& indexes) const	{
    indexes.clear();
    for (unsigned int i = 1; i < signal.size() - 1; i++){
	if(isPeak(signal, i)){
	    indexes.push_back(i);
	}
    }
}
Ejemplo n.º 4
0
// Looks for the given coordinates in the peak list of the gradient
// and returns the index+1 of it, if present. If not present, returns 0.
//
// Adds 1 to index so that the zero _index_ is returned as true, not false
int Gradient::peaks_list_contains(int i, int j){

    for( int n = 0; isPeak(n); ++n){
        if (getAnglesXCoord(n) == j &&
            getAnglesYCoord(n) == i){
            return n+1;
        }
    }
    return 0;
}
Ejemplo n.º 5
0
/**
 * Update the 2D grid of peak angles
 */
void Gradient::updatePeakGrid()
{
    clearPeakGrid();

    // Write angle to grid for each peak in list
    for(int i=0; isPeak(i); ++i){
        peaks[getAnglesYCoord(i) + Gradient::rows/2]
            [getAnglesXCoord(i) + Gradient::cols/2] =
            getAngle(i);
    }
}
Ejemplo n.º 6
0
 int peak(const vector<int> &num, const int left, const int right) {
     if (left > right)
         return -1;
     int mid = (left + right) / 2;
     if (isPeak(num, mid))
         return mid;
     else {
         int subleft = peak(num, left, mid - 1);
         if (subleft >= 0)
             return subleft;
         else return peak(num, mid + 1, right);
     }
 }
Ejemplo n.º 7
0
QSet<int> SpectrumAnalyzer::analyzeFT(int range, const QVector<float> &data)
{
	const float average = std::accumulate(data.begin(), data.end(), .0f) / data.size();
	const float threshold = qMax(50 * average, .1f * data.size());

	QSet<int> pressedKeys;

	for (int i = 1; i < data.size() / 2; ++i)
	{
		if (isPeak(&data[i], threshold) && !isPeak(&data[i/2], threshold))
		{
			const float freq = float(m_sampleRate) * i / data.size();
			const int nTasto = qRound(12 * log2(freq / 440) + 48);

			if (nTasto >= m_ranges[range]->firstKey && nTasto <= m_ranges[range]->lastKey)
				pressedKeys.insert(nTasto);
		}
	}

	emit spectrumAvailable(range, data, threshold);
	return pressedKeys;
}
Ejemplo n.º 8
0
void selectPeaks(peak_param *peak, map_t *kMap, double_arr *peakList, error **err)
{
  int bufferSize = peak->bufferSize;
  testError(bufferSize<=0, peak_badValue, "Buffer size should be at least 1.", *err, __LINE__); forwardError(*err, __LINE__,);
  
  double *kappa = kMap->kappa;
  double *fArr = peakList->array;
  int N1 = kMap->N1;
  int N2 = kMap->N2;
  
  int i, j, count = 0;
  for (j=bufferSize; j<N2-bufferSize; j++) {
    for (i=bufferSize; i<N1-bufferSize; i++) {
      if (isPeak(kappa, N1, i, j)) {
	fArr[count] = kappa[i+j*N1] / peak->sigma_noise;
	count++;
      }
    }
  }
  
  peakList->length = count;
  return;
}
Ejemplo n.º 9
0
void PeakDetectorNaiveImpl::findPeaks(const FrequencyData& fd, peakdata::Scan& result) const
{
    result.scanNumber = fd.scanNumber(); 
    result.retentionTime = fd.retentionTime(); 
    result.observationDuration = fd.observationDuration();
    result.calibrationParameters = fd.calibrationParameters();
    result.peakFamilies.clear();

    const double noiseLevel = sqrt(fd.variance());
    const double threshold = noiseLevel * noiseFactor_;

    for (FrequencyData::const_iterator it=fd.data().begin(); it!=fd.data().end(); ++it)
    if (isPeak(it, fd.data(), threshold, detectionRadius_))
    {
        result.peakFamilies.push_back(PeakFamily());
        PeakFamily& peakFamily = result.peakFamilies.back();
        peakFamily.peaks.push_back(Peak());
        Peak& peak = peakFamily.peaks.back();

        peak.frequency = it->x;
        peak.intensity = it->y.real();
        peak.phase = it->y.imag();
    }
}
Ejemplo n.º 10
0
bool Options::parse(int &argc, wchar_t **&argv)
{
    int ch, pos;
    while ((ch = wide::getopt_long(argc, argv,
                                   short_opts, long_options, 0)) != EOF)
    {
        if (ch == 'h')
            return usage(), false;
        else if (ch == 'chck')
            this->check_only = true;
        else if (ch == 'fmts')
            this->print_available_formats = true;
        else if (ch == 'o')
            this->ofilename = wide::optarg;
        else if (ch == 'd')
            this->outdir = wide::optarg;
        else if (ch < 0xff && (pos = strutil::strindex("cavV", ch)) >= 0) {
            if ((this->output_format && !isAAC()) || this->method != -1) {
                std::fputws(L"Encoding mode options are exclusive.\n", stderr);
                return false;
            }
            this->method = pos;
            if (std::swscanf(wide::optarg, L"%lf", &this->bitrate) != 1) {
                std::fputws(L"AAC Bitrate/Quality must be an integer.\n",
                            stderr);
                return false;
            }
        }
        else if (ch == 'A') {
            if ((this->output_format && !isALAC()) || this->method != -1) {
                std::fputws(L"Encoding mode options are exclusive.\n", stderr);
                return false;
            }
            this->output_format = 'alac';
        }
        else if (ch == 'D') {
            if (this->output_format && !isLPCM()) {
                std::fputws(L"Encoding mode options are exclusive.\n", stderr);
                return false;
            }
            this->output_format = 'lpcm';
        }
        else if (ch == 'aach') {
            if (this->output_format && !isAAC()) {
                std::fputws(L"--he is only available for AAC.\n", stderr);
                return false;
            }
            this->output_format = 'aach';
        }
        else if (ch == 'play') {
            if (this->output_format && !isWaveOut()) {
                std::fputws(L"--play cannot be specified with encoding mode.\n",
                            stderr);
                return false;
            }
            this->output_format = 'play';
        }
        else if (ch == 'peak') {
            if (this->output_format && !isPeak()) {
                std::fputws(L"--peak cannot be specified with encoding mode.\n",
                            stderr);
                return false;
            }
            this->output_format = 'peak';
        }
        else if (ch == 'caff')
            this->is_caf = true;
        else if (ch == 'q') {
            if (std::swscanf(wide::optarg, L"%u", &this->quality) != 1) {
                std::fputws(L"-q requires an integer.\n", stderr);
                return false;
            }
        }
        else if (ch == 'log ')
            this->logfilename = wide::optarg;
        else if (ch == 'nspd')
            this->no_smart_padding = true;
        else if (ch == 'nsrc') {
            this->native_resampler = true;
            if (wide::optarg) {
                strutil::Tokenizer<wchar_t> tokens(wide::optarg, L",");
                wchar_t *tok;
                while (tok = tokens.next()) {
                    int n;
                    if (std::swscanf(tok, L"%u", &n) == 1)
                        this->native_resampler_quality = n;
                    else if (std::wcslen(tok) == 4)
                        this->native_resampler_complexity =
                            util::fourcc(strutil::w2us(tok).c_str());
                    else {
                        std::fputws(L"Invalid arg for --native-resampler.\n",
                                    stderr);
                        return false;
                    }
                }
            }
        }
        else if (ch == 'N')
            this->normalize = true;
        else if (ch == 's')
            this->verbose = 0;
        else if (ch == 'verb')
            this->verbose = 2;
        else if (ch == 'S')
            this->save_stat = true;
        else if (ch == 'n')
            this->nice = true;
        else if (ch == 'thrd')
            this->threading = true;
        else if (ch == 'i')
            this->ignore_length = true;
        else if (ch == 'R')
            this->is_raw = true;
        else if (ch == 'ADTS')
            this->is_adts = true;
        else if (ch == 'noop')
            this->no_optimize = true;
        else if (ch == 'cat ')
            this->concat = true;
        else if (ch == 'nfmt')
            this->fname_format = wide::optarg;
        else if (ch == 'tmpd')
            this->tmpdir = wide::optarg;
        else if (ch == 'nmxn')
            this->no_matrix_normalize = true;
        else if (ch == 'cmap') {
            strutil::Tokenizer<wchar_t> tokens(wide::optarg, L",");
            wchar_t *tok;
            while ((tok = tokens.next()) != 0) {
                unsigned n;
                if (std::swscanf(tok, L"%u", &n) == 1)
                    this->chanmap.push_back(n);
                else {
                    std::fputws(L"Invalid arg for --chanmap.\n", stderr);
                    return false;
                }
            }
            uint32_t low = std::numeric_limits<int>::max();
            uint32_t high = 0;
            for (size_t i = 0; i < this->chanmap.size(); ++i) {
                uint32_t n = this->chanmap[i];
                if (n < low) low = n;
                if (n > high) high = n;
            }
            if (low < 1 || high > this->chanmap.size()) {
                std::fputws(L"Invalid arg for --chanmap.\n", stderr);
                return false;
            }
        }
        else if (ch == 'r') {
            if (!std::wcscmp(wide::optarg, L"keep"))
                this->rate = -1;
            else if (!std::wcscmp(wide::optarg, L"auto"))
                this->rate = 0;
            else if (std::swscanf(wide::optarg, L"%u", &this->rate) != 1) {
                std::fputws(L"Invalid arg for --rate.\n", stderr);
                return false;
            }
        }
        else if (ch == 'lpf ') {
            if (std::swscanf(wide::optarg, L"%u", &this->lowpass) != 1) {
                std::fputws(L"--lowpass requires an integer.\n", stderr);
                return false;
            }
        }
        else if (ch == 'b') {
            uint32_t n;
            if (std::swscanf(wide::optarg, L"%u", &n) != 1) {
                std::fputws(L"-b requires an integer.\n", stderr);
                return false;
            }
            if (n <= 1 || n > 32) {
                std::fputws(L"Bits per sample is too low or too high.\n",
                            stderr);
                return false;
            }
            this->bits_per_sample = n;
        }
        else if (ch == 'ndit') {
            this->no_dither = true;
        }
        else if (ch == 'mask') {
            if (std::swscanf(wide::optarg, L"%i", &this->chanmask) != 1) {
                std::fputws(L"--chanmask requires an integer.\n", stderr);
                return false;
            }
        }
        else if (ch == 'Rchn') {
            if (std::swscanf(wide::optarg, L"%u", &this->raw_channels) != 1) {
                std::fputws(L"--raw-channels requires an integer.\n", stderr);
                return false;
            }
            if (this->raw_channels == 0) {
                std::fputws(L"Invalid --raw-channels value.\n", stderr);
                return false;
            }
            if (this->raw_channels > 8) {
                std::fputws(L"--raw-channels too large.\n", stderr);
                return false;
            }
        }
        else if (ch == 'Rrat') {
            if (std::swscanf(wide::optarg, L"%u",
                             &this->raw_sample_rate) != 1) {
                std::fputws(L"--raw-rate requires an integer.\n", stderr);
                return false;
            }
            if (this->raw_sample_rate == 0) {
                std::fputws(L"Invalid --raw-rate value.\n", stderr);
                return false;
            }
        }
        else if (ch == 'Rfmt')
            this->raw_format = wide::optarg;
        else if (ch == 'afst')
            this->alac_fast = true;
        else if (ch == 'gain') {
            if (std::swscanf(wide::optarg, L"%lf", &this->gain) != 1) {
                std::fputws(L"--gain requires an floating point number.\n",
                            stderr);
                return false;
            }
        }
        else if (ch == 'drc ') {
            double threshold, ratio, knee, attack, release;
            if (std::swscanf(wide::optarg,
                             L"%lf:%lf:%lf:%lf:%lf",
                             &threshold,
                             &ratio,
                             &knee,
                             &attack,
                             &release) != 5) {
                std::fputws(L"--drc requires 5 parameters.\n", stderr);
                return false;
            }
            if (threshold >= 0.0) {
                std::fputws(L"DRC threshold cannot be positive.\n",
                            stderr);
                return false;
            }
            if (ratio <= 1.0) {
                std::fputws(L"DRC ratio has to be greater than 1.0\n",
                            stderr);
                return false;
            }
            if (knee < 0.0) {
                std::fputws(L"DRC knee width cannot be negative.\n",
                            stderr);
                return false;
            }
            if (attack < 0.0) {
                std::fputws(L"DRC attack time cannot be negative.\n",
                            stderr);
                return false;
            }
            if (release < 0.0) {
                std::fputws(L"DRC release time cannot be negative.\n",
                            stderr);
                return false;
            }
            wchar_t *p = wide::optarg;
            for (int i = 0; i < 5; ++i) {
                p = wcschr(p, L':');
                if (p) ++p;
            }
            this->drc_params.push_back(DRCParams(threshold, ratio, knee,
                                                 attack, release, p));
        }
        else if (ch == 'limt')
            this->limiter = true;
        else if (ch == 'from')
            this->start = wide::optarg;
        else if (ch == 'end ')
            this->end = wide::optarg;
        else if (ch == 'dlay')
            this->delay = wide::optarg;
        else if (ch == 'ndly')
            this->num_priming = 0;
        else if (ch == 'encd') {
            if (std::swscanf(wide::optarg, L"%u", &this->num_priming) != 1) {
                std::fputws(L"Invalid arg for --num-priming.\n", stderr);
                return false;
            }
            if (this->num_priming > 2112) {
                std::fputws(L"num-priming must not be greater than 2112.\n",
                            stderr);
                return false;
            }
        }
        else if (ch == 'soar')
            this->sort_args = true;
        else if (ch == 'gapm') {
            if (std::swscanf(wide::optarg, L"%u", &this->gapless_mode) != 1) {
                std::fputws(L"Invalid arg for --gapless-mode.\n", stderr);
                return false;
            }
        }
        else if (ch == 'txcp') {
            if (std::swscanf(wide::optarg, L"%u", &this->textcp) != 1) {
                std::fputws(L"--text-codepage requires code page number.\n",
                            stderr);
                return false;
            }
        }
        else if (ch == 'ctrk') {
            if (!strutil::parse_numeric_ranges(wide::optarg,
                                               &this->cue_tracks)) {
                std::fputws(L"Invalid arg for --cue-tracks.\n", stderr);
                return false;
            }
        }
        else if (ch == 'atsz') {
            if (std::swscanf(wide::optarg, L"%u", &this->artwork_size) != 1) {
                std::fputws(L"--artwork-size requires an integer.\n", stderr);
                return false;
            }
        }
        else if (ch == Tag::kArtwork)
            this->artwork_files.push_back(wide::optarg);
        else if (std::find(tag_keys, tag_keys_end, ch) != tag_keys_end) {
            if (ch != Tag::kCompilation)
                this->tagopts[ch] = wide::optarg;
            else if (!wide::optarg)
                this->tagopts[ch] = L"1";
            else {
                int n;
                if (std::swscanf(wide::optarg, L"%d", &n) != 1) {
                    std::fputws(L"Invalid --compilation option arg.\n", stderr);
                    return false;
                }
                this->tagopts[ch] = wide::optarg;
            }
        }
        else if (ch == 'tag ') {
            strutil::Tokenizer<wchar_t> tokens(wide::optarg, L":");
            wchar_t *key = tokens.next();
            wchar_t *value = tokens.rest();
            size_t keylen = std::wcslen(key);
            if (!value || (keylen != 3 && keylen != 4)) {
                std::fputws(L"Invalid --tag option arg.\n", stderr);
                return false;
            }
            uint32_t fcc = (keylen == 3) ? 0xa9 : 0;
            wchar_t wc;
            while ((wc = *key++) != 0) {
                if (wc != 0xa9 && (wc < 0x20 || wc > 0x7e)) {
                    std::fputws(L"Bogus util::fourcc for --tag.\n", stderr);
                    return false;
                }
                fcc = ((fcc << 8) | wc);
            }
            this->tagopts[fcc] = value;
        }
        else if (ch == 'ltag') {
            strutil::Tokenizer<wchar_t> tokens(wide::optarg, L":");
            wchar_t *key = tokens.next();
            wchar_t *value = tokens.rest();
            if (!value) {
                std::fputws(L"Invalid arg for --long-tag.\n", stderr);
                return false;
            }
            this->longtags[strutil::w2us(key)] = value;
        }
        else if (ch == 'chap')
            this->chapter_file = wide::optarg;
        else if (ch == 'mixp')
            this->remix_preset = wide::optarg;
        else if (ch == 'mixm')
            this->remix_file = wide::optarg;
        else if (ch == 'fftg')
            this->filename_from_tag = true;
        else
            return false;
    }
    argc -= wide::optind;
    argv += wide::optind;

    if (!argc && !this->check_only && !this->print_available_formats) {
        if (wide::optind == 1)
            return usage(), false;
        else {
            std::fputws(L"Input file name is required.\n", stderr);
            return false;
        }
    }
    if (argc > 1 && this->ofilename && !this->concat) {
        std::fputws(L"-o is not available for multiple output.\n", stderr);
        return false;
    }
    if (!this->output_format) {
#ifdef REFALAC
        this->output_format = 'alac';
#else
        this->output_format = 'aac ';
#endif
    }
    if (isSBR() && this->method == kTVBR) {
        std::fputws(L"TVBR is not available for HE.\n", stderr);
        return false;
    }
    if (isAAC() && this->method == -1) {
        this->method = isSBR() ? kCVBR : kTVBR;
        this->bitrate = isSBR() ? 0 : 90;
    }
    if (isMP4() && this->ofilename && !std::wcscmp(this->ofilename, L"-")) {
        std::fputws(L"MP4 piping is not supported.\n", stderr);
        return false;
    }
    if (!isAAC() && this->is_adts) {
        std::fputws(L"--adts is only available for AAC.\n", stderr);
        return false;
    }
    if (!isAAC() && this->quality != -1) {
        std::fputws(L"-q is only available for AAC.\n", stderr);
        return false;
    }
    if (this->is_caf && this->is_adts) {
        std::fputws(L"Can't use --caf and --adts at the same time.\n",
                    stderr);
        return false;
    }
    if (this->ignore_length && this->is_raw) {
        std::fputws(L"Can't use --ignorelength and --raw at the same time.\n",
                    stderr);
        return false;
    }
    if (this->concat && argc > 1 && !this->ofilename &&
            !this->isPeak() && !this->isWaveOut()) {
        std::fputws(L"--concat requires output filename (use -o option).\n",
                    stderr);
        return false;
    }
    if ((!isAAC() || isSBR()) && this->num_priming != 2112) {
        std::fputws(L"--num-priming is only applicable for AAC LC.\n", stderr);
        return false;
    }
    if (this->quality == -1)
        this->quality = 2;
    return true;
}
Ejemplo n.º 11
0
bool SimplePeakFinder::isPeak(const std::vector< std::vector<double> >& signal, unsigned int index1, unsigned int index2) const{
    return isPeak(signal[index1],index2);
}