数字信号产生之ARMA模型数据的产生

时间:2022-05-20 14:44:10

uniform.h

 

 

#pragma once

class uniform
{
private:
 double a, b, generate_num;
 int * seed;
 int s;
 int M, N, i, j;

public:
 uniform()
 {
  M = 1048576;
  N = 2045;
 }
 void generate();
 double random_number(double, double, int *);
};

double uniform::random_number(double a, double b, int * seed)
{
 (*seed) = N * (*seed) + 1;
 (*seed) = (*seed) - ((*seed) / M) * M;
 generate_num = static_cast<double>((*seed)) / M;
 generate_num = a + (b - a) * generate_num;
 return (generate_num);
}

 

 

gauss.h

 

#pragma once
#include "uniform.h"

uniform unif_num;

class gauss
{
private:
 double mean, sigma, x, y, generate_num;
 int s;
 int * seed;
 int i, j, m;

public:
 gauss() {}
 void generate();
 double random_number(double, double, int *);
};

double gauss::random_number(double mean, double sigma, int * seed)
{
 x = 0;
 for (m = 0; m < 12; m++)
 {
  x += unif_num.random_number(0.0, 1.0, seed);
 }
 x = x - 6.0;
 y = mean + x * sigma;
 return (y);
}

 

 

arma.h

 

#pragma once
#include "gauss.h"
#include <stdlib.h>

class arma
{
private:
 double mean, sigma, temp;
 double * a, * b, * w, * generate_num;
 int * seed;
 int s, p, q, n, i, j, k, l, m;

public:
 arma() {}
 void generate();
 void random_number(double *, double *, int, int, double, double, int *, double *, int);
 ~arma()
 {
  delete[] a, b, w, generate_num;
 }
};

void arma::random_number(double * a, double * b, int p, int q, double mean, double sigma, int * seed, double * x, int n)
{
 w = new double[n];
 gauss gau_num;

 for (k = 0; k < n; k++)
  w[k] = gau_num.random_number(mean, sigma, seed);
 generate_num[0] = b[0] * w[0];
 for (k = 1; k <= p; k++)
 {
  temp = 0.0;
  for (l = 1; l <= k; l++)
  {
   temp += a[l] * generate_num[k - l];
  }
  temp = b[0] * w[k] - temp;
  if (q == 0)
  {
   generate_num[k] = temp;
   continue;
  }
  m = (k > q) ? q : k;
  for (l = 1; l <= m; l++)
  {
   temp += b[l] * w[k - l];
  }
  generate_num[k] = temp;
 }
 for (k = (p + 1); k < n; k++)
 {
  temp = 0.0;
  for (l = 1; l <= p; l++)
  {
   temp += a[l] * generate_num[k - l];
  }
  temp = b[0] * w[k] - temp;
  if (q == 0)
  {
   generate_num[k] = temp;
   continue;
  }
  m = (k > q) ? q : k;
  for (l = 1; l <= m; l++)
  {
   temp += b[l] * w[k - l];
  }
  generate_num[k] = temp;
 }
}

 

 

 

arma.cpp

 

//产生200个ARMA(2,2)模型数据
#include <iostream>
#include <iomanip>
#include <fstream>
#include "ARMA.h"

using namespace std;

void main()
{
 arma solution;
 solution.generate();
}

void arma::generate()
{
 cout << "输入ARMA(p,q)模型数据的长度:";
 cin >> n;
 cout << "输入ARMA(p,q)模型的自回归阶数:";
 cin >> p;
 cout << "输入ARMA(p,q)模型的滑动平均阶数:";
 cin >> q;
 cout << "输入ARMA(p,q)模型的自回归系数:" << endl;
 a = new double[p + 1];
 for (i = 0; i <= p; i++)
 {
  cout << "a[" << i << "] = ";
  cin >> a[i];
 }
 cout << "输入ARMA(p,q)模型的滑动平均系数:" << endl;
 b = new double[q + 1];
 for (i = 0; i <= q; i++)
 {
  cout << "b[" << i << "] = ";
  cin >> b[i];
 }
 cout << "输入产生白噪声所用的正态分布的均值:";
 cin >> mean;
 cout << "输入产生白噪声所用的正态分布的均方差:";
 cin >> sigma;
 cout << "输入随机数的种子:";
 cin >> s;
 //存放ARMA(p,q)模型的数据
 generate_num = new double[n];
 
 random_number(a, b, p, q, mean, sigma, &s, generate_num, n);

 ofstream fout("ARMA.dat");
 for (i = 0; i < n; i++)
  fout << generate_num[i] << endl;
 fout.close();
}