def fft_unpack(org1, org2): o1, o2, a1, a2 = unpack(org1, org2) avg_len = (len(o1) + len(o2))/2.0 std_len = abs(len(o1) - avg_len) child_len = -1 while (child_len < 1): child_len = int(abs(gauss( avg_len, std_len ))) # Find the next power of 2 greater than the longest length if len(o1) > len(o2): pow = nextpow2(len(o1)) else: pow = nextpow2(len(o2)) # Deep copy tmp1 = [ele for ele in o1] tmp2 = [ele for ele in o2] # Zero-pad tmp1.extend([0 for i in range(pow - len(tmp1))]) tmp2.extend([0 for i in range(pow - len(tmp2))]) o1, o2 = tmp1, tmp2 o1 = fft(o1) o2 = fft(o2) return avg_len, std_len, child_len, pow, o1, o2, a1, a2
def fft_repack(avg_len, std_len, child_len, pow, c1, c2, a1, a2): # Zero-pad c1 and c2 if necessary c1.extend([0 for i in range(nextpow2(len(c1)) - len(c1))]) c2.extend([0 for i in range(nextpow2(len(c2)) - len(c2))]) # Invert fft c1 = ifft(c1) c2 = ifft(c2) #print "Truncated" c1 = c1[:child_len] c2 = c2[:child_len] # TODO -- try interpolating back down to a smaller (child_len) size? #print "fft crossover from:", \ # len(org1['org'][0]), len(org2['org'][0]), \ # "to", len(c1), len(c2) # Get rid of imaginary components... c1 = [ele.real for ele in c1] c2 = [ele.real for ele in c2] c1, c2 = repack(c1, c2, a1, a2) return c1, c2