Mercurial > hg > svcore
comparison base/RangeMapper.cpp @ 384:6f6ab834449d spectrogram-cache-rejig
* Merge from trunk
author | Chris Cannam |
---|---|
date | Wed, 27 Feb 2008 11:59:42 +0000 |
parents | 2b40f83e7627 |
children |
comparison
equal
deleted
inserted
replaced
337:a6fab10ff9e6 | 384:6f6ab834449d |
---|---|
58 // << value << " (minpos " << m_minpos << ", maxpos " << m_maxpos << ", minval " << m_minval << ", maxval " << m_maxval << ")" << std::endl; | 58 // << value << " (minpos " << m_minpos << ", maxpos " << m_maxpos << ", minval " << m_minval << ", maxval " << m_maxval << ")" << std::endl; |
59 return value; | 59 return value; |
60 } | 60 } |
61 | 61 |
62 LogRangeMapper::LogRangeMapper(int minpos, int maxpos, | 62 LogRangeMapper::LogRangeMapper(int minpos, int maxpos, |
63 float ratio, float minlog, | 63 float minval, float maxval, |
64 QString unit) : | 64 QString unit) : |
65 m_minpos(minpos), | 65 m_minpos(minpos), |
66 m_maxpos(maxpos), | 66 m_maxpos(maxpos), |
67 m_ratio(ratio), | |
68 m_minlog(minlog), | |
69 m_unit(unit) | 67 m_unit(unit) |
70 { | 68 { |
69 convertMinMax(minpos, maxpos, minval, maxval, m_minlog, m_ratio); | |
70 | |
71 std::cerr << "LogRangeMapper: minpos " << minpos << ", maxpos " | |
72 << maxpos << ", minval " << minval << ", maxval " | |
73 << maxval << ", minlog " << m_minlog << ", ratio " << m_ratio | |
74 << ", unit " << unit.toStdString() << std::endl; | |
75 | |
71 assert(m_maxpos != m_minpos); | 76 assert(m_maxpos != m_minpos); |
72 | 77 |
73 m_maxlog = (m_maxpos - m_minpos) / m_ratio + m_minlog; | 78 m_maxlog = (m_maxpos - m_minpos) / m_ratio + m_minlog; |
74 } | 79 } |
75 | 80 |
81 void | |
82 LogRangeMapper::convertMinMax(int minpos, int maxpos, | |
83 float minval, float maxval, | |
84 float &minlog, float &ratio) | |
85 { | |
86 static float thresh = powf(10, -10); | |
87 if (minval < thresh) minval = thresh; | |
88 minlog = log10f(minval); | |
89 ratio = (maxpos - minpos) / (log10f(maxval) - minlog); | |
90 } | |
91 | |
92 void | |
93 LogRangeMapper::convertRatioMinLog(float ratio, float minlog, | |
94 int minpos, int maxpos, | |
95 float &minval, float &maxval) | |
96 { | |
97 minval = powf(10, minlog); | |
98 maxval = powf(10, (maxpos - minpos) / ratio + minlog); | |
99 } | |
100 | |
76 int | 101 int |
77 LogRangeMapper::getPositionForValue(float value) const | 102 LogRangeMapper::getPositionForValue(float value) const |
78 { | 103 { |
79 float mapped = m_ratio * log10(value); | 104 int position = (log10(value) - m_minlog) * m_ratio + m_minpos; |
80 int position = lrintf(((mapped - m_minlog) / (m_maxlog - m_minlog)) | |
81 * (m_maxpos - m_minpos)); | |
82 if (position < m_minpos) position = m_minpos; | 105 if (position < m_minpos) position = m_minpos; |
83 if (position > m_maxpos) position = m_maxpos; | 106 if (position > m_maxpos) position = m_maxpos; |
84 // std::cerr << "LogRangeMapper::getPositionForValue: " << value << " -> " | 107 std::cerr << "LogRangeMapper::getPositionForValue: " << value << " -> " |
85 // << position << " (minpos " << m_minpos << ", maxpos " << m_maxpos << ", ratio " << m_ratio << ", minlog " << m_minlog << ")" << std::endl; | 108 << position << " (minpos " << m_minpos << ", maxpos " << m_maxpos << ", ratio " << m_ratio << ", minlog " << m_minlog << ")" << std::endl; |
86 return position; | 109 return position; |
87 } | 110 } |
88 | 111 |
89 float | 112 float |
90 LogRangeMapper::getValueForPosition(int position) const | 113 LogRangeMapper::getValueForPosition(int position) const |
91 { | 114 { |
92 float value = powf(10, (position - m_minpos) / m_ratio + m_minlog); | 115 float value = powf(10, (position - m_minpos) / m_ratio + m_minlog); |
93 // std::cerr << "LogRangeMapper::getValueForPosition: " << position << " -> " | 116 std::cerr << "LogRangeMapper::getValueForPosition: " << position << " -> " |
94 // << value << " (minpos " << m_minpos << ", maxpos " << m_maxpos << ", ratio " << m_ratio << ", minlog " << m_minlog << ")" << std::endl; | 117 << value << " (minpos " << m_minpos << ", maxpos " << m_maxpos << ", ratio " << m_ratio << ", minlog " << m_minlog << ")" << std::endl; |
95 return value; | 118 return value; |
96 } | 119 } |
97 | 120 |