-1

Suppose that we have a simple signal with just 1 bump around center and nothing in rest of points. Something like a Gaussian distribution. for instance See This. Okay, Then we divided this shape to some data point.

I want to convert this shape to a trapezoidal shape in c++. Something like this. As far as i know, It's just a kind of taking average mathematically. Well, how can i do it ? In addition, I use ROOT CERN to show the results ...

This is the main part of my code, But it's not work correctly ...

char Data[Data_Point][5]    

for (int k = 0; k < Data_Point ; k++){ // Data_Point is equal to 512, and so my array has just 512 member.

  h1 = 0.0;
  h2 = 0.0;
  H = 0.0;
  H1 = 0.0;
  H2 = 0.0;

  for (int l = 0; l <= L; l++){  // L is a const int and equal to 80.

    if (k + 1 < Data_Point){
      h1 += ((stoi(Data[k + l])) - Baseline / 20.0);  // Baseline is average of 20 data point of first of 512 data point, by this i'm trying to make more accuarcy by subtracting the baseline.
    } else {
      H1 = h1;
      cout << H1;
    }

    if (L + G + k + l < Data_Point){    // G is a Const int and equal to 15.
      h2 += ((stoi(Data[L + G + k + l])) - Baseline / 20.0);
    } else {
      H2 = h2;
      cout << H2;
    }

  }
  H = ((h2 - h1) / L);

}

RisingTime->Fill(H);

When i Run this code i encounter with error that say : Unhandled exception at 0x7731C54F in Pro1.exe: Microsoft C++ exception: std::invalid_argument at memory location 0x0019E854.

Anyone can help ? Any answer will be appreciated.

Elia
  • 31
  • 5
  • Please edit question and define "trapezoidal shape", this is unclear. What do you expect as output ? – kebs Apr 08 '19 at 12:13
  • @kebs --- Actually, Its a filter that convert a signal to a trapezoid shape, its so called " Trapezoidal filter". I'm trying to convert it !!! – Elia Apr 08 '19 at 12:20
  • @kebs --- The question is, How can i convert it to this : Something like this `[link] (https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcTVKd6f9bFcwaSMAbQcGna1mRGC0PTtt7mcOZp2EfwViwhILtJt)` – Elia Apr 09 '19 at 04:56
  • 1
    Firstly, you need to reduce the scope of what you are working on. Your code throws an exception that you don't seem to expect or handle well. Fix that part first. For that, extract a [mcve] (using consistent formatting!) or learn how to use a debugger to step through your code. As a new user here, also take the [tour] and read [ask]. – Ulrich Eckhardt Apr 09 '19 at 05:03
  • [Edit](https://stackoverflow.com/posts/55572804/edit) your question instead of giving that information in comments. – kebs Apr 09 '19 at 09:22
  • You are using [stoi](https://en.cppreference.com/w/cpp/string/basic_string/stol) with something that is not a string, but a pointer on a `char`! Very bad idea... And please indent your code correctly. – kebs Apr 09 '19 at 09:23
  • ^^^ Related: https://stackoverflow.com/questions/31928686/stoi-function-gives-error-stdinvalid-argument-at-memory-location-0x0035e8d8 Note that [`stoi`](https://en.cppreference.com/w/cpp/string/basic_string/stol) is not [`atoi`](https://en.cppreference.com/w/cpp/string/byte/atoi). – Bob__ Apr 11 '19 at 14:46

1 Answers1

0

I found it:

// Trapezoidal filter

for (int k = 0; k < Data_Point; k++){

    h1 = 0.0;
    h2 = 0.0;
    H = 0.0;
    H1 = 0.0;
    H2 = 0.0;

    for (int m = 0; m <= (L - 1); m++){

    if (k + m < Data_Point){
      h1 += ((stoi(Data[k + m])));
    }   //  if (k + m < Data_Point)
    else{
      H1 = h1;
      cout << H1;
    }   //  else

    if (L + G + k + m < Data_Point){
      h2 += ((stoi(Data[L + G + k + m])));
    }   //  if (L + G + k + m < Data_Point)
    else{
      H2 = h2;
      cout << H2;
    }   //  else

  } //  for (int m = 0; m <= (L - 1); m++)

  H = ((h2 - h1) / L);
}
Keldorn
  • 1,980
  • 15
  • 25
Elia
  • 31
  • 5