How to construct a preemptive digital filter to neutralize an unwanted filter I'm trying to think of how to construct a filter based on the following scenario. I have some time trace with a certain known power spectral density (I can verify this with a periodogram of the time trace). The time trace then goes into a machine that realizes it, which does so with some level of imperfection. Treating the device as a black box, it is essentially a filter that is applied to the time trace, and produces a slightly adjusted power spectral density. This power spectral density I can then measure with a spectrum analyzer.

So I'm wondering, can I apply a preemptive filter to my time trace, before it goes into the black box, so that I get my desired power spectral density? I suppose I should have all the information I need as I know the power spectral density before and after the black box, which should somehow allow me to construct a type of inverse filter. How exactly I am not sure of however, and that is my main question.

I should also note that I tried a much less clean method. I took the power spectral density after the black box, and simply mirrored it, and used this as a preemptive filter. This actually seems to work quite well in the sense that the resulting PSD has the correct shape (it was sloping downwards before while it should be straight, which this method achieves), but the amplitudes are no longer correct and rescaling the data is problematic. So I figured a more analytic approach would be required.
If $S_x(\omega)$ is the desired input power spectral density (PSD), and $S_y(\omega)$ is the PSD at the output of the black box, which has the (unknown) frequency response $H(\omega)$, then the following relation holds:

$$S_y(\omega)=S_x(\omega)|H(\omega)|^2\tag{1}$$

Now you want to cascade the black box with a filter to recover the original PSD $S_x(\omega)$ from the distorted PSD $S_y(\omega)$:

$$S_y(\omega)|G(\omega)|^2=S_x(\omega)\tag{2}$$

From $(2)$ it immediately follows that

$$|G(\omega)|=\sqrt{\frac{S_x(\omega)}{S_y(\omega)}},\quad S_y(\omega)\neq 0\tag{3}$$

You need to design a filter with magnitude response $|G(\omega)|$ given by $(3)$. Note that for this application, the filter's phase response is irrelevant.