mx_real_t getShimmer(fextract_series_t *pulses, mx_real_t *signal, int n_samples, mx_real_t maxPitch, mx_real_t minPitch) { mx_real_t pmin =0.8/maxPitch, pmax=1.25/minPitch; mx_real_t maximumPeriodFactor=1.3, maximumAmplitudeFactor=1.6; mx_real_t result; fextract_series_t *peaks=NULL; peaks = getPeaks(pulses,signal,n_samples,pmin,pmax,maximumPeriodFactor); if (peaks == NULL) return -1; result = getShimmer_local (peaks, pmin, pmax, maximumAmplitudeFactor); series_destroy(peaks); return result; }
void PoldiPeakSearch::exec() { g_log.information() << "PoldiPeakSearch:" << std::endl; Workspace2D_sptr correlationWorkspace = getProperty("InputWorkspace"); MantidVec correlationQValues = correlationWorkspace->readX(0); MantidVec correlatedCounts = correlationWorkspace->readY(0); g_log.information() << " Auto-correlation data read." << std::endl; Unit_sptr xUnit = correlationWorkspace->getAxis(0)->unit(); if (xUnit->caption() == "") { g_log.information() << " Workspace does not have unit, defaulting to MomentumTransfer." << std::endl; xUnit = UnitFactory::Instance().create("MomentumTransfer"); } else { g_log.information() << " Unit of workspace is " << xUnit->caption() << "." << std::endl; } setMinimumDistance(getProperty("MinimumPeakSeparation")); setMinimumPeakHeight(getProperty("MinimumPeakHeight")); setMaximumPeakNumber(getProperty("MaximumPeakNumber")); if (m_doubleMinimumDistance > static_cast<int>(correlatedCounts.size())) { throw(std::runtime_error("MinimumPeakSeparation is smaller than number of " "spectrum points - no peaks possible.")); } g_log.information() << " Parameters set." << std::endl; MantidVec summedNeighborCounts = getNeighborSums(correlatedCounts); g_log.information() << " Neighboring counts summed, contains " << summedNeighborCounts.size() << " data points." << std::endl; std::list<MantidVec::const_iterator> peakPositionsSummed = findPeaks(summedNeighborCounts.begin(), summedNeighborCounts.end()); g_log.information() << " Peaks detected in summed spectrum: " << peakPositionsSummed.size() << std::endl; /* This step is required because peaks are actually searched in the * "sum-of-neighbors"-spectrum. * The mapping removes the offset from the peak position which results from * different beginning * of this vector compared to the original correlation counts. */ std::list<MantidVec::const_iterator> peakPositionsCorrelation = mapPeakPositionsToCorrelationData(peakPositionsSummed, summedNeighborCounts.begin(), correlatedCounts.begin()); g_log.information() << " Peak positions transformed to original spectrum." << std::endl; /* Since intensities are required for filtering, they are extracted from the * original count data, * along with the Q-values. */ std::vector<PoldiPeak_sptr> peakCoordinates = getPeaks(correlatedCounts.begin(), correlatedCounts.end(), peakPositionsCorrelation, correlationQValues, xUnit); g_log.information() << " Extracted peak positions in Q and intensity guesses." << std::endl; UncertainValue backgroundWithSigma = getBackgroundWithSigma(peakPositionsCorrelation, correlatedCounts); g_log.information() << " Calculated average background and deviation: " << UncertainValueIO::toString(backgroundWithSigma) << std::endl; if ((*getProperty("MinimumPeakHeight")).isDefault()) { setMinimumPeakHeight(minimumPeakHeightFromBackground(backgroundWithSigma)); } std::vector<PoldiPeak_sptr> intensityFilteredPeaks(peakCoordinates.size()); auto newEnd = std::remove_copy_if( peakCoordinates.begin(), peakCoordinates.end(), intensityFilteredPeaks.begin(), boost::bind(&PoldiPeakSearch::isLessThanMinimum, this, _1)); intensityFilteredPeaks.resize( std::distance(intensityFilteredPeaks.begin(), newEnd)); g_log.information() << " Peaks above minimum intensity (" << m_minimumPeakHeight << "): " << intensityFilteredPeaks.size() << std::endl; std::sort(intensityFilteredPeaks.begin(), intensityFilteredPeaks.end(), boost::bind<bool>(&PoldiPeak::greaterThan, _1, _2, &PoldiPeak::intensity)); for (std::vector<PoldiPeak_sptr>::const_iterator peak = intensityFilteredPeaks.begin(); peak != intensityFilteredPeaks.end(); ++peak) { m_peaks->addPeak(*peak); } /* The derived background error is set as error in the workspace containing * correlation data, so it may be used as weights for peak fitting later on. */ setErrorsOnWorkspace(correlationWorkspace, backgroundWithSigma.error()); setProperty("OutputWorkspace", m_peaks->asTableWorkspace()); }
int main(int argc, char **argv) { //void main() { FILE *output; //keypoint output (text) Image *sourceImage, *sourceImage2; Image *doubledImage; Image *currentImage; Octave *octavesList; Octave *temp, *current; KeyPoint *peaks; KeyPoint *counter, *prevPoint; KeyPoint *keypoints, *keyCounter; int candidates = 0; double scale; double ***mags, ***directions; int octaveCount; int tempHeight, tempWidth, tempDepth; int totalKeypoints = 0; double imageSigma; char fileName[32] = {'\0'}; FILE *candidateOutput; FILE *trashOutput; FILE *filteredOutput; /* if(argc < 2) { printf("Usage: sift dat_file\n"); return 0; } */ /* read input */ //sourceImage = readPGMFile(argv[1]); sourceImage = readDATFile("./image.dat"); if(sourceImage == NULL) { printf("file was not read\n"); return 1; } scale = START_SCALE; /* print constants used */ printf("Constants:\n"); printf("START_SIGMA=%f\n", START_SIGMA); printf("OCTAVE_LIMIT=%d\n", OCTAVE_LIMIT); printf("IMAGES_PER_OCTAVE=%d\n", IMAGES_PER_OCTAVE); printf("SKIP_OCTAVES=%d\n", SKIP_OCTAVES); printf("MIN_SIZE=%d\n", MIN_SIZE); printf("MAX_ADJ_STEPS=%d\n", MAX_ADJ_STEPS); printf("EDGE_RATIO=%f\n", EDGE_RATIO); printf("CONTRAST_THRESH=%f\n", CONTRAST_THRESH); printf("CAP=%f\n", CAP); /* double image to save data */ doubledImage = doubleImage(sourceImage); if(doubledImage == NULL) { return 1; } scale /= 2.0; printf("\nImage doubled\n"); /* preprocessing smoothing*/ printf("Preprocessing smoothing\n"); doubledImage = gaussianConvolution2DFast(doubledImage, 1.0); // doubledImage = gaussianConvolution2DFast(sourceImage, 1.0); /* build octaves of scalespace */ printf("Octave pyramid construction\n"); currentImage = doubledImage; octavesList = NULL; octaveCount = 0; while(octaveCount < OCTAVE_LIMIT && currentImage->width >= MIN_SIZE && currentImage->height >= MIN_SIZE) { if(currentImage == NULL) { printf(" ***Current image null. Exiting.\n"); return 1; } /* create octave pointer wrapper */ printf(" Octave for pic %d %d (%d)\n", currentImage->width, currentImage->height, octaveCount); temp = (Octave *) malloc(sizeof(Octave)); if(temp == NULL) { printf(" ***Out of memory. Exiting.\n"); return 1; } /* build scale space L(x,y,sigma) */ printf(" Gaussian Scale Space\n"); if(octaveCount == 0) imageSigma = 1.0; //blur of the first image is 1.0 else imageSigma = START_SIGMA; temp->gaussianSpace = buildScaleSpaceOctave(currentImage, scale, imageSigma); /* build diff space D(x,y,sigma) */ printf(" Diff Gaussian Space\n"); temp->diffGaussianSpace = buildDifferences(temp->gaussianSpace); /* store and go on */ octavesList = link(octavesList, temp); currentImage = halveImage(getLastImage(temp->gaussianSpace)); scale *= 2.0; ++octaveCount; printf(" Image halved\n"); } --octaveCount; /* one extra */ /* generate keypoints from each octave of scalespace */ printf("Keypoint generation\n"); candidateOutput = fopen("output_candidates.txt", "w"); //helpful output file trashOutput = fopen("output_trash.txt", "w"); //helpful output file filteredOutput = fopen("output_filtered.txt", "w"); //helpful output file histOutput = fopen("output_histogram.txt", "w"); //outputs histograms histOutput2 = fopen("output_histogram2.txt", "w"); //turn on/off in scalespace.c keypoints = NULL; sourceImage2 = cloneImage(sourceImage); for(current = octavesList; current != NULL && octaveCount >= SKIP_OCTAVES; current = current->next, --octaveCount) { printf(" Octave %d\n", octaveCount); fprintf(histOutput, "Octave %d\n", octaveCount); fprintf(histOutput, "---------\n"); fprintf(histOutput2, "Octave %d\n", octaveCount); fprintf(histOutput2, "---------\n"); /* find keypoint candidates */ printf(" Looking for peaks\n"); peaks = getPeaks(current); /* temporary output */ scale = current->diffGaussianSpace->imageScale; printPoints(candidateOutput, peaks, scale, octaveCount); markImageSPoint(peaks, sourceImage2, scale); //output candidates if(peaks != NULL) printf(" found %d candidates\n", peaks->count); else printf(" found 0 candidates\n"); /* filter peaks for contrast and edge-iness and localize them */ printf(" Filtering and localizing\n"); trash = NULL; peaks = filterAndLocalizePeaks(current->diffGaussianSpace, current->gaussianSpace, peaks); /* temporary output */ printPoints(filteredOutput, peaks, scale, octaveCount); printTrashed(trashOutput, trash, scale, octaveCount); if(peaks != NULL) printf(" have %d candidates remaining\n", peaks->count); else printf(" have 0 candidates remaining\n"); /* precompute magnitudes and orientations */ printf(" Generating magnitudes and orientations\n"); tempHeight = current->gaussianSpace->imgHeight; tempWidth = current->gaussianSpace->imgWidth; tempDepth = current->gaussianSpace->imageCount - 2; current->gaussianSpace->magnitudes = allocate3D(tempHeight, tempWidth, tempDepth); current->gaussianSpace->directions = allocate3D(tempHeight, tempWidth, tempDepth); getMagnitudesAndOrientations(current->gaussianSpace); /* assign orientation(s) */ printf(" Assigning orientation(s)\n"); generateKeypoints(current->gaussianSpace, peaks); /* generate descriptors*/ printf(" Calculating descriptors\n"); createDescriptors(current->gaussianSpace, peaks); /* multiply by imgScale to get real values */ for(counter = peaks; counter != NULL; counter = counter->next) { counter->finalX *= counter->imgScale; counter->finalY *= counter->imgScale; //also multiply x,y? counter->scale *= counter->imgScale; } /* merge lists */ if(peaks != NULL) { if(keypoints != NULL) { keypoints->listRear->next = peaks; keypoints->listRear = peaks->listRear; } else keypoints = peaks; } /* deallocate memory */ deallocate3D(current->gaussianSpace->magnitudes, tempHeight, tempWidth); deallocate3D(current->gaussianSpace->directions, tempHeight, tempWidth); /* deallocate trashed points */ counter = trash; prevPoint = NULL; while(counter != NULL) { prevPoint = counter; counter = counter->next; free(prevPoint); } } fclose(histOutput); fclose(histOutput2); /* write footer to each file */ printFooter(candidateOutput); fclose(candidateOutput); printFooter(trashOutput); fclose(trashOutput); printFooter(filteredOutput); fclose(filteredOutput); /* output keypoint descriptors */ printf("Output\n"); output = fopen("output.txt", "w"); for(keyCounter = keypoints; keyCounter != NULL; keyCounter=keyCounter->next) { ++totalKeypoints; } fprintf(output, "%d %d\n", totalKeypoints, 128); for(keyCounter = keypoints; keyCounter != NULL; keyCounter=keyCounter->next) { printKeyPoint(keyCounter, output); } fclose(output); /* output keypoints on image*/ markImage(keypoints, sourceImage); writeDATFile(sourceImage, "./output/keypoints.dat"); /* output initial candidates on image */ writeDATFile(sourceImage2, "./output/keypoints_all.dat"); printf("Done. %d keypoints found.\n", totalKeypoints); free(sourceImage); free(sourceImage2); free(doubledImage); //free octaves - should put it here return 0; }