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.
My understanding is that a spectrum analyzer is a graph of frequency vs magntiude and a spectrogram is a heatmap of frequency vs time. It might help to clarify which one you're after.

For audio, 1024 - 4096 is an adequate frame size to yield good results at 44.1kHz and extremly fast to compute. You will almost certainly need to window each frame.

For a spectrum analyzer, you'll have to decide if you want to skip samples for each animation frame (not to be consufsed with your FFT frame - lol) or make sure you compute multiple frames to cover all samples that have been output since the last animation frame and take, say, the max for each bin. This won't be an issue for a spectrogram as you'll decide how many lines of your heatmap you can draw since your last animation frame.

For a spectrum analyzer you'll need some ballistics so each bin can "fall".

Although you can use the STFT to calculate a spectrogram, you will have a time-frequency trade-off. The longer your frame, the better your frequency resolution at the expense of time resolution.

Alternative approaches:

* use the constant Q transform
* use a filterbank