Skip to content

Commit 24afbfe

Browse files
committed
convolution norm fixing
1 parent f9bb12b commit 24afbfe

File tree

3 files changed

+32
-15
lines changed

3 files changed

+32
-15
lines changed

examples/convolution/convolute_functions.inl

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,8 @@
4747
#include <hydra/functions/Ipatia.h>
4848
#include <hydra/device/System.h>
4949
#include <hydra/functions/ConvolutionFunctor.h>
50-
#include <hydra/Random.h>
50+
#include <hydra/GaussKronrodQuadrature.h>
51+
5152
//hydra
5253
#if HYDRA_DEVICE_SYSTEM == CUDA
5354
#include <hydra/CuFFT.h>
@@ -112,9 +113,9 @@ int main(int argv, char** argc)
112113

113114
// gaussian
114115
auto mean = hydra::Parameter::Create( "mean").Value(0.0).Error(0.0001);
115-
auto sigma = hydra::Parameter::Create("sigma").Value(3.5).Error(0.0001);
116+
auto sigma = hydra::Parameter::Create("sigma").Value(1.0).Error(0.0001);
116117

117-
hydra::Gaussian<double> gaussian_kernel(mean, sigma);
118+
hydra::Gaussian<double> kernel(mean, sigma);
118119

119120
//===========================
120121
// signals
@@ -162,7 +163,7 @@ int main(int argv, char** argc)
162163
auto start_d = std::chrono::high_resolution_clock::now();
163164

164165
hydra::convolute(hydra::device::sys, fft_backend,
165-
signal, gaussian_kernel, min, max, conv_result, true);
166+
signal, kernel, min, max, conv_result, true);
166167

167168
auto end_d = std::chrono::high_resolution_clock::now();
168169

@@ -176,7 +177,22 @@ int main(int argv, char** argc)
176177
/*
177178
* using the hydra::ConvolutionFunctor
178179
*/
179-
auto convoluton = hydra::make_convolution<double>( hydra::device::sys, fft_backend, signal, gaussian_kernel, min, max, conv_result.size() );
180+
auto convoluton = hydra::make_convolution<double>( hydra::device::sys, fft_backend,
181+
signal, kernel, min, max, conv_result.size() );
182+
183+
hydra::GaussKronrodQuadrature<61,100, hydra::device::sys_t> GKQ61_Kernel(-25.0, 25.0);
184+
hydra::GaussKronrodQuadrature<61,100, hydra::device::sys_t> GKQ61_Signal(min, max);
185+
186+
auto kernel_int = GKQ61_Kernel.Integrate(kernel);
187+
auto signal_int = GKQ61_Signal.Integrate(signal);
188+
auto convol_int = GKQ61_Signal.Integrate(convoluton);
189+
190+
std::cout << "===========================================" << std::endl;
191+
std::cout << "Kernel Integral: " <<kernel_int.first <<"+/-"<< kernel_int.second << std::endl;
192+
std::cout << "Signal Integral: " <<signal_int.first <<"+/-"<< signal_int.second << std::endl;
193+
std::cout << "Convol Integral: " <<convol_int.first <<"+/-"<< convol_int.second << std::endl;
194+
std::cout << "===========================================" << std::endl;
195+
180196

181197
//------------------------
182198
//------------------------
@@ -193,7 +209,7 @@ int main(int argv, char** argc)
193209
hist_convol_functor->SetBinContent(i, convoluton(hist_convol_functor->GetBinCenter(i)) );
194210
hist_convol->SetBinContent(i, conv_result[i-1] );
195211
hist_signal->SetBinContent(i, signal(hist_signal->GetBinCenter(i) ) );
196-
hist_kernel->SetBinContent(i, gaussian_kernel( hist_kernel->GetBinCenter(i)));
212+
hist_kernel->SetBinContent(i, kernel( hist_kernel->GetBinCenter(i)));
197213
}
198214
#endif //_ROOT_AVAILABLE_
199215

hydra/Convolution.h

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,6 @@ convolute(detail::BackendPolicy<BACKEND> policy, detail::FFTPolicy<T, FFTBackend
7979

8080
int nsamples = std::forward<Iterable>(output).size();
8181

82-
8382
T delta = (max - min)/(nsamples);
8483

8584
auto complex_buffer = hydra_thrust::get_temporary_buffer<complex_type>(policy, nsamples+1);
@@ -88,20 +87,23 @@ convolute(detail::BackendPolicy<BACKEND> policy, detail::FFTPolicy<T, FFTBackend
8887

8988
//
9089
auto counting_samples = range(0, 2*nsamples);
90+
9191
// sample kernel
9292
auto kernel_sampler = hydra::detail::convolution::KernelSampler<Kernel>(kernel, nsamples, delta);
9393

9494
hydra_thrust::transform(policy, counting_samples.begin(), counting_samples.end(),
9595
kernel_samples.first , kernel_sampler);
9696

97-
auto norm_factor = hydra_thrust::reduce(policy, kernel_samples.first, kernel_samples.first + kernel_samples.second );
97+
//auto norm_factor = hydra_thrust::reduce(policy, kernel_samples.first, kernel_samples.first + kernel_samples.second );
9898

9999
// sample function
100100
auto functor_sampler = hydra::detail::convolution::FunctorSampler<Functor>(functor, nsamples, min, delta);
101101

102102
hydra_thrust::transform( policy, counting_samples.begin(), counting_samples.end(),
103103
functor_samples.first, functor_sampler);
104104

105+
//norm_factor *= hydra_thrust::reduce(policy, functor_samples.first, functor_samples.first + functor_samples.second );
106+
105107
//transform kernel
106108
auto fft_kernel = _RealToComplexFFT( kernel_samples.second );
107109

@@ -131,19 +133,19 @@ convolute(detail::BackendPolicy<BACKEND> policy, detail::FFTPolicy<T, FFTBackend
131133

132134
//transform product back to real
133135

134-
auto fft_product = _ComplexToRealFFT( 2*nsamples );
136+
auto fft_product = _ComplexToRealFFT( 2.0*nsamples );
135137

136138
fft_product.LoadInputData(complex_buffer.second, complex_buffer.first);
137139
fft_product.Execute();
138140

139141
auto fft_product_output = fft_product.GetOutputData();
140142

141-
T n = 2*nsamples*norm_factor;
143+
T n = 2*nsamples;
142144

143145
auto normalize_fft = detail::convolution::NormalizeFFT<T>(n);
144146

145147
auto first = hydra_thrust::make_transform_iterator( fft_product_output.first,normalize_fft);
146-
auto last = hydra_thrust::make_transform_iterator(fft_product_output.first + nsamples+1,normalize_fft);
148+
auto last = hydra_thrust::make_transform_iterator(fft_product_output.first + nsamples,normalize_fft);
147149

148150
auto fft_product_range = make_range(first, last);
149151

hydra/detail/Convolution.inl

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -70,12 +70,11 @@ struct KernelSampler
7070

7171
KernelSampler(Kernel const& kernel, int nsamples , double delta):
7272
fDelta(delta),
73-
7473
fKernel(kernel)
7574
{
7675
fNZero = nsamples;
77-
fNMin = nsamples- nsamples/16;
78-
fNMax = nsamples+ nsamples/16;
76+
fNMin = nsamples- nsamples/2;
77+
fNMax = nsamples+ nsamples/2;
7978
}
8079

8180
__hydra_host__ __hydra_device__
@@ -215,7 +214,7 @@ struct FunctorSampler
215214

216215
double t = index*fDelta + fMin;
217216

218-
if( (t >= fMin) && ( t <= (fNSamples)*fDelta + fMin)){
217+
if( (t >= fMin) && ( t < (fNSamples)*fDelta + fMin)){
219218

220219
value = fFunctor(t);
221220
}

0 commit comments

Comments
 (0)