void adjust(int i) { int p, c, e = heap[i]; p = i, c = i*2; while(c<=hn) { if(c<hn && minFreq(heap[c+1], heap[c])) c++; if(minFreq(heap[c], e)) heap[p] = heap[c], p = c, c = p * 2; else break; } heap[p] = e; }
void FrequencyPlot::update(int w, int h) { m_image.load(w, h, 1); m_rect = {10, 10, (w | 1) - 40, (h | 1) - 20}; m_width_rcp = 1.0f / float(m_rect.width()); fill(&m_image, Color(0)); for(int x=0; x<m_rect.width(); x++) { if(!(x & 1)) { int xx = x + m_rect.x(); m_image(xx, m_rect.y())[0] = m_image(xx, m_rect.bottom() - 1)[0] = 255; } } float maxfreqlog = log10(maxFreq()); m_min_freq_log = log10(minFreq()); m_freq_range_log = maxfreqlog - m_min_freq_log; m_freq_range_log_rcp = 1.0f / m_freq_range_log; int n = 1; float f = 1.0f; float df = 1.0f; for(;;) { if(f >= minFreq()) { if(f >= maxFreq()) break; int x = (log10(f) - m_min_freq_log) * m_freq_range_log_rcp * m_rect.width(); paintLineAt(x + m_rect.x(), n == 1 ? 127 : 63); } n++; f += df; if(n == 10) { n = 1; df *= 10; } } paintLineAt(m_rect.x(), 255); paintLineAt(m_rect.right()-1, 255); }
void insert(int e) { int p, c; heap[++hn] = e, c = hn, p = hn/2; while(c>0) if(minFreq(e, heap[p])) heap[c] = heap[p], c = p, p = c / 2; else break; heap[c] = e; }
float FrequencyPlot::freqAt(int x) const { if(x < m_rect.x() || x >= m_rect.right()) return 0.0f; return pow(10, float(x - m_rect.x()) * m_width_rcp * m_freq_range_log) + minFreq(); }