// Build a waveform of type `type` with frequency `t` and amplitude `a` in `temp_cycle`, // Then call swap_cycle() to replace `cycle` with `temp_cycle`. void build_tone(float t, float a, enum TYPE type) { temp_samples_per_cycle = (sample_rate / t); sample_t scale = 2 * PI / temp_samples_per_cycle; temp_cycle = (sample_t *) malloc(temp_samples_per_cycle * sizeof(sample_t)); if (temp_cycle == NULL) { fprintf(stderr,"Memory allocation failed.\n"); exit(3); } int i; for (i = 0; i < temp_samples_per_cycle; i++) { switch(type) { case Sine: temp_cycle[i] = a * sin(i * scale); break; case Square: temp_cycle[i] = a * sgn(sin(i * scale)); break; case Triangle: temp_cycle[i] = a * (asin(sin(i * scale)) / (PI / 2)); break; case Sawtooth: temp_cycle[i] = a * (2 * ((((float) i) / temp_samples_per_cycle) - floor(((float) i) / temp_samples_per_cycle)) - 1); break; } } swap_cycle(); }
void StaticPersistence<D, CT, OT, E, Cmp>:: initialize(const Filtration& filtration) { order_.assign(filtration.size(), OrderElement()); rLog(rlPersistence, "Initializing persistence"); OffsetMap<typename Filtration::Index, iterator> om(filtration.begin(), begin()); for (typename Filtration::Index cur = filtration.begin(); cur != filtration.end(); ++cur) { Cycle z; BOOST_FOREACH(const typename Filtration::Simplex& s, std::make_pair(cur->boundary_begin(), cur->boundary_end())) z.push_back(index(om[filtration.find(s)])); z.sort(ocmp_); iterator ocur = om[cur]; swap_cycle(ocur, z); set_pair(ocur, ocur); } }
void StaticPersistence<D, CT, OT, E, Cmp>:: pair_simplices(iterator bg, iterator end, bool store_negative, const Visitor& visitor) { #if LOGGING typename ContainerTraits::OutputMap outmap(order_); #endif // FIXME: need sane output for logging rLog(rlPersistence, "Entered: pair_simplices"); for (iterator j = bg; j != end; ++j) { visitor.init(j); rLog(rlPersistence, "Simplex %s", outmap(j).c_str()); Cycle z; swap_cycle(j, z); rLog(rlPersistence, " has boundary: %s", z.tostring(outmap).c_str()); // Sparsify the cycle by removing the negative elements if (!store_negative) { typename OrderElement::Cycle zz; BOOST_FOREACH(OrderIndex i, z) if (i->sign()) // positive zz.push_back(i); z.swap(zz); } // -------------------------- CountNum(cPersistencePairBoundaries, z.size()); #ifdef COUNTERS Count(cPersistencePair); #endif // COUNTERS while(!z.empty()) { OrderIndex i = z.top(ocmp_); // take the youngest element with respect to the OrderComparison rLog(rlPersistence, " %s: %s", outmap(i).c_str(), outmap(i->pair).c_str()); // TODO: is this even a meaningful assert? AssertMsg(!ocmp_(i, index(j)), "Simplices in the cycle must precede current simplex: (%s in cycle of %s)", outmap(i).c_str(), outmap(j).c_str()); // i is not paired, so we pair j with i if (iterator_to(i->pair) == iterator_to(i)) { rLog(rlPersistence, " Pairing %s and %s with cycle %s", outmap(i).c_str(), outmap(j).c_str(), z.tostring(outmap).c_str()); set_pair(i, j); swap_cycle(j, z); set_pair(j, i); CountNum(cPersistencePairCycleLength, j->cycle.size()); CountBy (cPersistencePairCycleLength, j->cycle.size()); break; } // update element z.add(i->pair->cycle, ocmp_); visitor.update(j, iterator_to(i)); rLog(rlPersistence, " new cycle: %s", z.tostring(outmap).c_str()); } // if z was empty, so is (already) j->cycle, so nothing to do visitor.finished(j); rLog(rlPersistence, "Finished with %s: %s", outmap(j).c_str(), outmap(j->pair).c_str()); }