I'm having a hard time reproducing your issue. I've thrown together a toy problem in both numpy and nfftd in an attempt to replicate your issue with amplitudes, and I'm failing.
Toy Problem
I've calculated a discrete sin wave (10 points), and have put the sin wave through the transform you describe above, though I've replaced the random function with a random array that doesn't change from one iteration to the next.
Python Code
First, the python code:
# create the discrete sin wave
buf = np.sin(np.linspace(0,2*np.pi,10))
# run through transform described by sebpiq
amp = np.abs(np.fft.rfft(buf))
ph = np.array([ 3.69536029, 1.99564315, 1.046197 , 4.43086754, 0.01415843, 3.53100037])
ph = ph*1j
amp = amp * np.exp(ph)
buf = np.fft.irfft(amp)
And the results:
array([-0.28116423, -0.8469374 , -1.11143881, -0.68594442, -0.04085493,
0.60202526, 0.4990367 , 0.85927706, 0.76606064, 0.23994014])
Javascript Code
Second, see the equivalent javascript code:
// Require stuff
var math = require('mathjs');
var ndfft = require("ndfft");
// setup sin(x) in the real part, and 0 in the imag part
var re = [ 0.00000000e+00, 6.42787610e-01, 9.84807753e-01, 8.66025404e-01, 3.42020143e-01, -3.42020143e-01, -8.66025404e-01, -9.84807753e-01, -6.42787610e-01, -2.44929360e-16]
var im = [0,0,0,0,0,0,0,0,0,0]
// Cache a "random" matrix for easy comparison
ph = [ 3.69536029, 1.99564315, 1.046197 , 4.43086754, 0.01415843, 3.53100037, 0.01420613, 4.19132513, 1.08002181, 3.05840211];
// Run algorithm
ndfft(1,re,im);
amplitudes = math.epow(math.add(math.epow(re, 2), math.epow(im, 2)), 0.5);
re = math.emultiply(math.cos(ph), amplitudes);
im = math.emultiply(math.sin(ph), amplitudes);
ndfft(-1,re,im);
And the results:
> re
[ -0.44298344101499465,
-1.0485812598130462,
-1.028287331663926,
-0.37462920250565557,
0.5543077299497436,
0.7410571497545398,
0.7829965195020553,
0.26939736089453314,
0.3029516683194694,
-2.440823114672447e-16 ]
> im
[ -0.019894821927674437,
0.027734906190559794,
-0.0766942109405363,
-0.017488411630453154,
0.04089362484484916,
-0.17252218798632196,
-0.11135041005265467,
-0.008717609033075929,
0.5669181583191372,
2.0352312370257754e-17 ]
Magnitude of Results
As far as I can see, the results are of very similar magnitude. The average magnitude of the python result is 0.593 and the average magnitude of the javascript result is 0.592. Have I gone wrong somewhere along the way?
Thanks,
Spencer
Update on Normalization
I don't find any normalization issues as mentioned by Jaime with either code base. One of the first things I tried was a forward fft of a sin wave, immediately followed by a backward fft of the result, and in both numpy and nfftd, the results were properly normalized.