Thanks for the challenge of making a MWE. It turns out that some of my thinking was wrong. This is what I should have been doing:
- Transform to frequency domain
- Set negative frequency components to zero
- Multiply positive frequency components by 2 (do not change zero frequency component and take care with the Nyquist frequency component)
- Transform back to time domain
- Take real part of the analytic signal
Ergo, you have the original signal.
As an example
using FFTW
# Create signal
dt = 0.2
t = 0:dt:1
y = sin.(2*pi*t)
df = 1/t[end]
yf = fft(y)
# Make signal one sided like rfft
n = length(t)
np = div(n, 2) + 1
yf[np+1:end] = zeros(ComplexF64, n-np)
# Put energy lost from zeroing -ve frequency back in
if iseven(n)
yf[2:np-1] = 2*yf[2:np-1]
else
yf[2:np] = 2*yf[2:np]
end
# Inverse FFT
ynew = ifft(yf)
[y ynew] # compare y with real part of ynew
MATLAB does the same thing