0

I'm trying to make a code to implement the direct type 2 realisation structure from Digital Signal Processing in C++.

So what I have is this for a class and of course a header that belongs to it:

#include "DirectType2.h"

DirectType2::DirectType2()
{
}

DirectType2::DirectType2(vector<double> inputBCoefficients, vector<double> inputACoefficients)
{
    inputXFunction = inputACoefficients;
    inputYFunction = inputBCoefficients;
}

void DirectType2::setInputFunctions(vector<double> inputA, vector<double> inputB)
{
    inputXFunction = inputA;
    inputYFunction = inputB;
}

vector<double> DirectType2::getInputXFunction()
{
    return inputXFunction;
}

vector<double> DirectType2::getInputYFunction()
{
     return inputYFunction;
}

double DirectType2::xOfNFunction(unsigned int n)
{
    double sum = 0;

    for (unsigned int i = 1; i <= n || i < inputXFunction.size(); i++)
    {
        sum += inputXFunction[i] * xOfNFunction(n - i);
    }

    double x = inputXFunction[0] + sum;

    return x;
}

double DirectType2::wOfNFunction(unsigned int n)
{
    double w = 0;

    double sum = 0;

    for (unsigned int i = 1; (i < n || i < inputXFunction.size()); i++)
    {
        sum -= inputXFunction[i] * wOfNFunction(n - i);
    }

    w = inputXFunction[1] - sum;


    return w;
}

double DirectType2::yOfNFunction(unsigned int n)
{
    double y = 0;

for (unsigned int i = 0; i < n || i < inputYFunction.size(); i++)
{
    y += inputYFunction[i] * wOfNFunction(n - i);
}

    return y;
}


DirectType2::~DirectType2()
{
}

My source file looks like this:

#include <iostream>
#include "DirectType2.h"

using namespace std;

int main()
{
    double myDoubleY[] = { 0.01031, 0.06188, 0.1547, 0.2063, 0.1547, 0.06188, 0.01031 };
    double myDoubleX[] = { 1, -1.188, 1.305, -0.6743, 0.2635, -0.05175, 0.005023 };
    vector<double> yFunction(myDoubleY, myDoubleY + sizeof(myDoubleY) / sizeof(double));
    vector<double> xFunction(myDoubleX, myDoubleX + sizeof(myDoubleX) / sizeof(double));

    DirectType2 port(yFunction, xFunction);
    cout << "y(n) med n = 6 bliver: " << port.yOfNFunction(6) << endl;

    return 0;
}

Now my issue is that when I run this code, a file called "xutility" pops up and throws this exception:

Unhandled exception at 0x01284B77 in Portefølje 2 - forsøg 2.exe: 0xC00000FD: Stack overflow (parameters: 0x00000001, 0x00E02F64).

Now of course I understand that it is a stack overflow that happens, but I dont understand why it opens this file and throws an exception from it, seeings as I don't really feel like I ever use this file..

The code snippet that throws the exception is:

const _Ty2& _Get_second() const noexcept
        {   // return const reference to second
        return (_Myval2);
        }

and if I try to let the code continue it says this:

Exception thrown at 0x01284B77 in Portefølje 2 - forsøg 2.exe: 0xC0000005:
Access violation writing location 0x00E00FB4.

Edit:

In the header I have defined two vectors called inputXFunction and inputYFunction both set to contain doubles.

T. Aalbaek
  • 23
  • 1
  • 10
  • Remove `using namespace std;` and add `std::` where necessary. Who knows what you're opening yourself up to there! – Ian Oct 31 '18 at 14:53
  • @Ian good idea for a solution! But unfortunately it didn't work.. – T. Aalbaek Oct 31 '18 at 15:01
  • Ok T. Nevertheless, it's generally advised you don't add `using namespace std;` See [why is using namespace std considered bad?](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice) for further info. – Ian Oct 31 '18 at 15:03
  • Oh okay! Thanks for the advice! :) Yeah, I am kinda of a rookie – T. Aalbaek Oct 31 '18 at 15:03
  • Surely you need `&&` instead of `||` to terminate the for-loops? You need both conditions to be true while looping, so if one goes false, it should terminate. – Ian Oct 31 '18 at 15:12
  • You're right @Ian incidently that fixed it! Thanks man! You're a champ! – T. Aalbaek Oct 31 '18 at 15:14
  • Always pass on the thanks by up-voting and accepting ;) – Ian Oct 31 '18 at 15:21

1 Answers1

0

Each of your for-loops should use && instead of || as you need both conditions to be met in order to continue accessing the arrays safely. E.g.:

for (unsigned int i = 1; (i < n && i < inputXFunction.size()); i++)
{
    sum -= inputXFunction[i] * wOfNFunction(n - i);
}

instead of:

for (unsigned int i = 1; (i < n || i < inputXFunction.size()); i++)
{
    sum -= inputXFunction[i] * wOfNFunction(n - i);
}
Ian
  • 1,172
  • 1
  • 17
  • 29