2
$\begingroup$

I was trying to simulate data for a 2 pole low pass filter inorder to solve with kalman filter to estimate the true states. With the below code I was unable to generate true states that look like sin function. I took x(t) = A0sin(wct) with wc = 2pi*fc, fc is cutoff frequency. Can some one help me regarding this?

I am trying to simulate a sinosoid and here how the result looks: result of code

The simulated data of state space model does not look like a sinsoid (x(t)). Can someone guide if I am looking the result worng way?

F =

    1.0000    0.0001
   -0.0012    1.0000

eig(F)

ans =

   1.0000 + 0.0004i
   1.0000 - 0.0004i

The state space model looks like

\begin{eqnarray} X_k &=& F X_{k-1}+ q_k \nonumber \\ Z_k &=& H X_k + \nu_k \end{eqnarray}

clear all; close all; clc;

% Define the parameters
A0 = 1;
fc = 0.5;
w = 2*pi*fc;

% Define the continuous-time state-space representation
A = [0, 1; -w^2, 0];


H = [1, 0];

% Convert the continuous-time state-space representation to a discrete-time state-space representation
fs = 8192;
Ts = 1/fs;
F = expm(A*Ts);

% Preallocate the arrays for the state and the measurements
N = 3*fs;
x_array = zeros(2,N);
z_array = zeros(1,N);

VarMnoise = 0.15;
vk = (sqrt(VarMnoise)*randn(N,2)); 


VarqNoise =0.25; % Variance of Q (proportinal to height of A0);
qk = (sqrt(VarqNoise)*randn(N,1)); 

% Initialize the state
x = [0; 0];

% Generate the measurements
for k = 1:N
    x = F*x + qk(k,:);
    z = H*x + vk(k);
    
    % Store the state and the measurements
    x_array(:,k) = x;
    z_array(k) = z;
end

% Plot the results
t = Ts*(0:N-1);
figure;
plot(t, x_array(1,:), 'LineWidth', 2);
hold on;
plot(t, z_array, 'LineWidth', 2);
legend('State', 'Measurements');
xlabel('Time (s)');
ylabel('Value');
$\endgroup$
3
  • $\begingroup$ "I was unable to" -- how? Please edit your question with the plots that you did generate, and tell us how what you see doesn't match what you expect. Printing out your calculated $\mathbf F$ and it's eigenvalues would be useful, too. $\endgroup$
    – TimWescott
    Commented Feb 12, 2023 at 23:56
  • $\begingroup$ Hi @TimWescott, I edited the quesiton and updated with plots. I am expecting to simulate a sinsoid that looks like x(t) = sin(w0*t). But the plots looks too vague. Or I might be interpreting things a wrong way fro the plots. $\endgroup$
    – Sagar
    Commented Feb 13, 2023 at 7:21
  • $\begingroup$ Are you after generating Sine like signal using a linear state model like in Linear Kalman? $\endgroup$
    – Royi
    Commented Feb 26, 2023 at 6:58

1 Answer 1

0
$\begingroup$

You have a zero-damping (hopefully) harmonic oscillator with a noise input, starting with both states equal to zero. It's period is two seconds and you're only simulating it for three seconds. Try giving it a run time of at least 20 seconds (10 periods) -- that should give you some opportunity to see something interesting.

Note that because it's an undamped oscillator fed with noise, it's amplitude will be the absolute value of a random walk function.

Finally, you're probably just exceeding the precision of your output, but you may want to check that abs(eig(F)) - 1 is very close to zero, and isn't positive. It's almost a doesn't-care because it's a nonstationary process with, ultimately, an infinite variance, but there is a difference between a random walk and a slowly growing exponential, at least eventually.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.