Question: I have a set of measurement which I want to model with 2nd-order difference equations (first order eqs don't model well enough).

The equation is 

$$y[n] = \alpha\_1 y[n-1] + \alpha\_2 y[n-2] + \beta\_0 x[n] + \beta\_1 x[n-1]$$

I want a generic formula for the impulse response.

I did a derivation, but being a total DSP beginner, I'm not sure whether it's correct or I'm missing some bits:

The transfer function is:

$$\begin{align}
H(z) &= \displaystyle \frac{Y(z)}{X(z)} \\
 &= \frac{\beta\_0 + \beta\_1 z^{-1}}{1 - \alpha\_1 z^{-1} - \alpha\_2 z^{-2}} \\
 &= \frac{\beta\_0 z^2 + \beta\_1 z}{z^2-\alpha\_1 z -\alpha\_2 } \\
 &= \frac{A\_1 \, z}{z-p\_1} + \frac{A\_2 \, z}{z-p\_2} \\
 &= A\_1\frac{1}{1-p\_1 z^{-1}} + A\_2\frac{1}{1-p\_2 z^{-1}} \\
\end{align}$$

where $p\_{1,2}$ are the poles and $A\_1$ and $A\_2$ can be calculated using partial fraction expansion.

Therefore the impulse response is

$h[n] = \big(A\_1 p\_1^n + A\_2 p\_2^n \big) u[n], $ 

where $u[n]$ is the discrete step function.

Is this ok?

Answer: 
Spectrograms are normally computed using the [Short-Time Fourier Transform](https://en.wikipedia.org/wiki/Short-time_Fourier_transform) (STFT), and can be done in real-time. It consists of splitting the signal into overlapped frames, using a window function (typically scaled cosine like Hann) and then computing the FFT for the frame.

The overlap and windowing is key to avoid 'spectral leakage', and sounds like what is missing in your case.
It actually depends on what you are trying to achieve. I have two general points here to help you:

First, you need to know the specifications of your real-time system. How long do you need to update your spectrogram, $\Delta t$?

Second, you need to decide your STFT properties (especially the overlap percent) so you can match your computation time with your target real-time update, $\Delta t$. The least possible computation time is when you have an overlap of $0$ or hop size is $L$ ($L$ is the window size), meaning every sampling time, $T\_s$, you will update your STFT property. If you have a powerful computation, matching $T\_s$ with $\Delta t$ is fine but not recommended due to bulky memory. In my spectrogram below, done in python, I use an overlap of $50\%$ (which is the usual percent) so I have a waiting time more than $T\_s$ to compute and update my STFT. The longest time possible is when you have overlap of $L - 1$ (minimum possible hop size is $1$).

By the way, the overlap percent determines the resolution of your spectrogram so you also need to plan it out.

[![spectrogram](https://i.stack.imgur.com/vRVx5.gif)](https://i.stack.imgur.com/vRVx5.gif)