Hey All,
I’m trying to recreate the convolution animation for the two square waves on Wikipedia found here:
en.wikipedia.org/wiki/Convolution
I’ve pretty well done everything but I can’t get the convolution function (fg)(t) to be drawn progressively as the waves pass each other. Instead ROOT draws (fg)(t) over the entire range of the plot resulting as a constant line at the value of the integral of the product of the two functions.
I think this is due to my in ability to find a way to make the function convolvedFunc() (see code below) depend on x. I could store the values of the integral in a TGraph and then draw it afterwards, but I want to be able to draw the resulting convoluted function in real time and use the resulting TF1 to do fits.
Thanks for your thoughts!
-Chris
//This can be run in ROOT by doing .x convolution.C
#include <iostream>
using namespace std;
//Instantiate the functions to be used
//Their properties will be set later
TF1 *func1; //The stationary Function
TF1 *func2; //The Function to be moved
TF1 *func3; //The product of the two functions
TF1 *func4; //The Resulting function
//Square Wave
Double_t squareWave(Double_t *x, Double_t *par){
Double_t xx = x[0];
if (xx < par[0]-par[1]/2.)
return 0;
else if (xx >= par[0]-par[1]/2. && xx <= par[0]+par[1]/2.)
return par[2];
else if (xx > par[0]+par[1]/2.)
return 0;
} //End Square Wave
//Does the multiplication of the two functions
Double_t multipliedFuncs(Double_t *x, Double_t *par){
Double_t xx = x[0];
Double_t f_x = func1->Eval(xx)*func2->Eval(xx);
return f_x;
}//End multipliedFuncs
//The resulting function of the convolution
Double_t convolvedFunc(Double_t *x, Double_t *par){
return func3->Integral(-2,2);
}
void convolution(){
//Create the First Function
func1 = new TF1("SquareWave",squareWave,-2,2,3);
func1->SetNpx(10000);
//Set the Parameters
func1->SetParameter(0,0); //Center of the Square
func1->SetParameter(1,1); //Width of the Square
func1->SetParameter(2,1); //Height of the Square
//Create the Second Function
func2 = new TF1("SquareWave2",squareWave,-2,2,3);
func2->SetNpx(10000);
//Set the Parameters
func2->SetParameter(0,-2); //Starting Center of the Square
func2->SetParameter(1,1); //Width of the Square
func2->SetParameter(2,1); //Height of the Square
//Amount by which to move the second function every step
Double_t moveOver = 0 .05;
//Create the Multiplied Function
func3 = new TF1("multipliedFuncs",multipliedFuncs,-2,2,0);
func3->SetNpx(10000);
func3->SetLineColor(kYellow);
func3->SetLineWidth(0);
func3->SetFillColor(kYellow);
func3->SetFillStyle(1001);
//Create Convolution Function
func4 = new TF1("convolvedFunc",convolvedFunc,-2,2,0);
func4->SetLineColor(kBlack);
//Loop to move the second function toward the first
for (Int_t i=0; i<75; i++){
//Set the Mean of the Guassian - This moves the function
func2->SetParameter(0,func2->GetParameter(0)+moveOver);
//Draw the Functions
func1->Draw();
func2->Draw("SAME");
func3->Draw("SAME");
func4->Draw("SAME");
gPad->Update();
}
}