Hi, below is my code to do a Convolution. I am tryingt o convolute my theoretical data with a Gaussian?

I seem to be getting a number of errors and its killing me

Cheers for any help Gareth

#include <iostream>
#include <cmath>
#include <math.h>
using std::cout;
using std::cin;
using std::endl;


	double getTheoreticalData(double x);
{
   double value;
   for (int x = 0; x <= 40; x++){
   // For straightforward square root
   value = sqrt(x);
   return (value);
   }
}

int main() {

   
   int mean;                  // the mean is the mean for the calculation of the Gaussian
   double StandardDeviation;      // the standard deviation for the calculation of the Gaussian
   double PI;                  // the value PI which will be defined by me as (3.141592654)
   double Gauss[41];               // the figure of the Gaussian I am trying to get
   double PISQ;                  // 2PI squared
   double exponential;            // this will be the exponential side of the Gauss equation
   double SDsq;                  // the 2 time SDsquared
   
   int sampleCount = 40;
   int gaussianSize = 40;

   StandardDeviation = 1;
   mean = 20;
   PI = 3.141592654;

   PISQ = (2 * PI) * ( 2 * PI );

   SDsq = ( StandardDeviation * StandardDeviation) * 2;

   // Create Gaussian
   for (int x = 0; x <= gaussianSize; ++x )
   {
      exponential = exp (( x - mean ) * ( x - mean ) * -1 ) / SDsq;
      Gauss[x] = ( 1 / (StandardDeviation * PISQ) * exponential );
      cout << Gauss[x] << endl;
   }

   // Now do the convolution

   // Loop through all 40 samples
   for ( int i = 0; i < sampleCount; ++i )
	
   {
      y[i] = 0;                       // set to zero before sum

      // Loop through convolution for this data point and perform sum
      for (int j = 0; j < gaussianSize; ++j )
      {
         y[i] += z[i - j] * Gauss[j];    // convolve: multiply and accumulate
      }
      cout << i << ": " << y[i] << endl;
   }

   return 0;

Err#1: this semicolon here: double getTheoreticalData(double x); This is not necesarry: double PI; Since you have included "cmath" header, you already have M_PI defined, and it's more precise than your PI!

Err#2,3: y[] is not declared, z[] is not declared
Err#4: after return 0; you need closing bracket '}' for main()

double y[41]?

I'm not checking a logic of your program since I don't really know what it's about, but first set your syntax straight, then fix logical errors

#include <iostream>
#include <cmath>
#include <math.h>
using std::cout;
using std::cin;
using std::endl;
using namespace std;



int main() {

   
   int mean;                  // the mean is the mean for the calculation of the Gaussian
   double StandardDeviation;      // the standard deviation for the calculation of the Gaussian
   double PI;                  // the value PI which will be defined by me as (3.141592654)
   double Gauss[41];               // the figure of the Gaussian I am trying to get
   double PISQ;                  // 2PI squared
   double exponential;            // this will be the exponential side of the Gauss equation
   double SDsq;                  // the 2 time SDsquared




   int sampleCount = 40;
   int gaussianSize = 40;

      StandardDeviation = 1;
   mean = 20;
   PI = 3.141592654;

   PISQ = (2 * PI) * ( 2 * PI );

   SDsq = ( StandardDeviation * StandardDeviation) * 2;

   // Create Gaussian
   for (int x = 0; x <= gaussianSize; ++x )
   {
      exponential = exp (( x - mean ) * ( x - mean ) * -1 ) / SDsq;
      Gauss[x] = ( 1 / (StandardDeviation * PISQ) * exponential );
      cout << Gauss[x] << endl;
   }


   

   // CONVOLUTION

	for (int i = 0; i < sampleCount; i++ )
	{

for (int j = 0; j < gaussianSize; ++j )
{
	double y[40];
	double function[40] = { 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,10,1,1,1,1,1,1,1,1,1,1,1,10, 1,1,1,1,1,1,1,1,1,1,1};

	y[i] = 0;
	
		y[i] += function[i - j] * Gauss[j];

}

cout << i << ": " << y[i] << endl;

	
}
   return 0;
   }

this is my current attempt

it comes up with errors on my final cout statement?

any ideas?

You have declared y inside for loop, so that means it only exists in for loop. Declare it before for!

Still working on this, can you believe it?

#include <cstdio>
#include <iostream>
#include <convolution.h>

#include <cmath>
using std::cout;
using std::cin;
using std::endl;
using namespace std;


int main()
{



   
   int mean;                  // the mean is the mean for the calculation of the Gaussian
   double StandardDeviation;      // the standard deviation for the calculation of the Gaussian
   double PI;                  // the value PI which will be defined by me as (3.141592654)
   double Gauss[41];               // the figure of the Gaussian I am trying to get
   double PISQ;                  // 2PI squared
   double exponential;            // this will be the exponential side of the Gauss equation
   double SDsq;                  // the 2 time SDsquared




   int sampleCount = 40;
   int gaussianSize = 40;

      StandardDeviation = 1;
   mean = 20;
   PI = 3.141592654;

   PISQ = (2 * PI) * ( 2 * PI );

   SDsq = ( StandardDeviation * StandardDeviation) * 2;

   // Create Gaussian
   for (int x = 0; x <= gaussianSize; ++x )
   {
      exponential = exp (( x - mean ) * ( x - mean ) * -1 ) / SDsq;
      Gauss[x] = ( 1 / (StandardDeviation * PISQ) * exponential );
      cout << Gauss[x] << endl;
   }


   

///////////////////////////////////////////////////////////////////////////////
// 1D convolution
// We assume input and kernel signal start from t=0.
///////////////////////////////////////////////////////////////////////////////
bool convolve1D(float* function, float* convolution, int dataSize, float* Gauss, int GaussSize)
{



    int i, j, k;

	i = 0;
	j = 1;

    // check validity of params
    if(!function || !out || !Gauss) return false;
    if(dataSize <=0 || GaussSize <= 0) return false;

    // start convolution from out[kernelSize-1] to out[dataSize-1] (last)
    for(i = GaussSize-1; i < dataSize; ++i)
    {
        convolution[i] = 0;                             // init to 0 before accumulate

        for(j = i, k = 0; k < GaussSize; --j, ++k)
            convolution[i] += function[j] * Gauss[k];
    }

    // convolution from out[0] to out[kernelSize-2]
    for(i = 0; i < GaussSize - 1; ++i)
    {
        convolution[i] = 0;                             // init to 0 before sum

        for(j = i, k = 0; j >= 0; --j, ++k)
            convolution[i] += function[j] * Gauss[k];

			cout << i << " , " << convolution[i] << endl;
	}

    
	return true;
}



    return 0;
}

im getting the error

--------------------Configuration: Convolutionsolved - Win32 Debug--------------------
Compiling...
Convolutionsolved.cpp
F:\Convolutionsolved.cpp(58) : error C2601: 'convolve1D' : local function definitions are illegal
Error executing cl.exe.

any ideas please

looks like it is something to do with the bool

thanks

You're missing a closing brace at the end of main(), so the compiler thinks you're trying to implement a function inside the body of a function - which is illegal.

This article has been dead for over six months. Start a new discussion instead.