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.
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);
}
User contributions licensed under CC BY-SA 3.0