24 #ifndef _MATCHEDFILTER_HPP_
25 #define _MATCHEDFILTER_HPP_
39 namespace MatchedFilter {
42 template <
typename X,
typename Y>
54 template <
typename space_type>
82 typename samples_type::size_type sampleCount =
samples.size();
84 if (result < 0) result = 0;
85 if (result > (
int)(sampleCount-1)) result = sampleCount-1;
86 return (
typename samples_type::size_type)(result);
96 template <
typename space_type>
97 std::ostream& operator<<(std::ostream& os, const SampledData<space_type>& data)
99 os <<
"[" << data.domain.first <<
"," << data.domain.second <<
"] "
100 <<
"(" << data.samples.size() <<
" samples)\n";
103 for (
unsigned int index=0; index!=data.samples.size(); ++index, ++it)
104 os << data.x(index) <<
"\t" << *it << std::endl;
110 template <
typename Kernel>
125 template <
typename X,
typename Y>
134 template <
typename Kernel>
142 typename KernelTraitsBase<Kernel>::space_type::abscissa_type
x;
143 typename KernelTraitsBase<Kernel>::space_type::ordinate_type
y;
149 template <
typename Kernel>
153 void (KernelConcept<Kernel>::*dummy)() = &KernelConcept<Kernel>::check;
158 template <
typename Y>
174 std::ostream& operator<<(std::ostream& os, const Correlation<Y>& c)
176 os <<
"<" << c.dot <<
", " << c.e2 <<
", " << c.angle() <<
">";
181 template <
typename Kernel>
196 template <
void(*T)()>
struct Dummy;
197 typedef Dummy< &checkKernelConcept<Kernel> >
dummy;
204 template <
typename Kernel>
211 checkKernelConcept<Kernel>();
214 for (
int i=-sampleRadius; i<=sampleRadius; i++)
215 filter.push_back(kernel(i*dx - shift));
221 inline double norm(
double d) {
return d*d;}
222 inline double conj(
double d) {
return d;}
225 template <
typename Filter>
228 double normalization = 0;
229 for (
typename Filter::const_iterator it=filter.begin(); it!=filter.end(); ++it)
230 normalization +=
norm(*it);
231 normalization = sqrt(normalization);
233 for (
typename Filter::iterator it=filter.begin(); it!=filter.end(); ++it)
234 *it /= normalization;
238 template <
typename Kernel>
239 std::vector<typename KernelTraits<Kernel>::filter_type>
245 checkKernelConcept<Kernel>();
248 std::vector<filter_type> filters;
250 for (
int i=0; i<subsampleFactor; i++)
251 filters.push_back(
createFilter(kernel, sampleRadius, dx, dx*i/subsampleFactor));
253 for_each(filters.begin(), filters.end(), normalizeFilter<filter_type>);
259 template<
typename Kernel>
266 checkKernelConcept<Kernel>();
270 for (; samples!=samplesEnd; ++samples, ++filter)
272 result.
dot += (*samples) *
conj(*filter);
273 normData +=
norm(*samples);
276 double normDot =
norm(result.
dot);
277 result.
e2 = (
std::max)(normData - normDot, 0.);
278 result.
tan2angle = normDot>0 ? result.
e2/normDot : std::numeric_limits<double>::infinity();
285 template<
typename Kernel>
286 typename KernelTraits<Kernel>::correlation_data_type
288 const Kernel& kernel,
292 checkKernelConcept<Kernel>();
298 if (data.
samples.empty())
return result;
299 result.samples.resize((data.
samples.size()-1) * subsampleFactor + 1);
309 unsigned int sampleIndex = sampleRadius;
310 for (
typename samples_type::const_iterator itData = data.
samples.begin() + sampleRadius;
311 itData + sampleRadius != data.
samples.end(); ++itData, ++sampleIndex)
312 for (
unsigned int filterIndex=0; filterIndex<filters.size(); ++filterIndex)
314 unsigned int index = sampleIndex * filters.size() + filterIndex;
316 if (index >= result.samples.size())
319 details::computeCorrelation<Kernel>(itData-sampleRadius, itData+sampleRadius+1,
320 filters[filterIndex].begin(),
321 result.samples[index]);
333 #endif // _MATCHEDFILTER_HPP_