# SNR computation in frequency domain with scipy.fftpack.fft

I have a noisy time series (gaussian coloured noise) to which I add a signal. I call the sum of noise+signal a segment. In the picture you can see an instance:

I want to compute the signal-to-noise ratio (NOT the logarithmic version) of the injected signal in the frequency domain using python's scipy.fftpack.fft module. So I wrote the following script.

def snr_inj_f(segment, noise, f_samp, f_max=2048):

N = len(segment)        # number of points in time series
T = 1.0 / f_samp        # sample spacing
x_f = fftfreq(N, T)
frequencies = x_f[0:N//2]     # only positive frequencies
h = segment-noise       # signal, obtained removing noise from segment

segment_f = scipy.fftpack.fft(segment)[0:N//2]   # segment fft only positive f
noise_f = scipy.fftpack.fft(noise)[0:N//2]       # noise fft
h_f = scipy.fftpack.fft(h)[0:N//2]               # signal fft

h_f =  h_f[where(frequencies<=f_max)]            # if an upper cut on frequency is desired
noise_f = noise_f[where(frequencies<=f_max)]
Norm = shape(where(frequencies<=f_max))[1]

snr_sq = np.sum(abs(h_f)**2/abs(noise_f)**2)     # SNR^2 as sum of ratio of squared fft coeff.
snr_f = sqrt(snr_sq)

return snr_f


However, I have doubts of doing things correctly. Since the number of samples N and the sampling frequency is the same, is it right to just basically sum over all the ratios of the psd bins? I am assuming that the $$PSD$$ is represented by the coefficients of the projection of a discrete time series $$x_n(t)$$, $$\tilde x_k$$, in the form: $$PSD = \frac{1}{T} *|\frac{\tilde x_k}{f_{samp}}|^2$$, where $$\tilde x_k$$ are the fast Fourier transform coefficients, not normalized. Since the duration of the time series $$T$$ and the sampling frequency $$f_samp$$ are the same for signal and noise, is it correct? Or am I missing a factor due to to the fact that I am only taking positive frequencies?

Furthermore: 1) Will whitening using, say, 300 previous seconds containing only noise, improve the SNR? 2) How should these SNR in frequency computation differ from the one in time domain? (Basically taking ratio of sum of squares of signal and noise samples)