# Simplest way to generate AR(2) process on MATLAB

As part of a project I need to use autocorrelation method of estimating model paramters of an autoregressive process on MATLAB.

Can anyone tell me the simplest way to generate an AR(2) process on MATLAB so that I can estimate its model parameters $$\hat{a}_{p}(k)$$ and $$\hat{b}_{0}$$?

The difference equation for the process is given below:

$$x(n) = -0.9x(n-1) + w(n)$$

• Hi Max! It's better if you close your previous question before asking a new one. I believe I have answered it but you place absolutely no response after getting your answer. Thank you for your understanding. Nov 6, 2018 at 9:41
• I owe you an apology for not going back and checking for the edit you made. Thanks for reminding! I'll keep that in mind next time. Nov 6, 2018 at 11:33
• Ok. Not a big deal, but a proper way to use this site... Nov 6, 2018 at 18:01

The simplest way to approximate an AR-2 process in Matlab / Octave is the following:

N = 1024;                    % number of process samples.
a = [1, -0.9, 0.2];          % denominator coefficients, p = 2.
b = [1.0];                   % numerator coefficient.
x = filter(b,a, randn(1,N)); % generate N sample of AR-2 x[n].


Note: an AR process requires a true-white noise sequence $$v[n]$$ at the input of the filter but here we input a single instance of a crude approximation of it. Hence the process is not truly an AR-2 but an approximation...

• So if I replace randn with awgn, I might get a truer white noise. I suppose that should be fine? Nov 6, 2018 at 18:45
• No they use the same function randn to generate the noise. Nov 6, 2018 at 19:10
• Ok one more doubt, how is the third element of a = [1 -0.9 0.2] chosen? Can we decide the value randomly or is it unique? Nov 8, 2018 at 7:11
• @MaxFrost that was just a random example Ar-2 system. In you application you should determine a0, a1 and a2. a0 = 1 by definition, so AR-2 process needs two coefficients a1 and a2 to be given or determined. For the difference equation $$y[n] + \alpha y[n-1] = x[n]$$ $a_1=-\alpha$ and $a_2 = 0$ ;i.e. that's an AR-1 system indeed. Nov 8, 2018 at 10:29