def fft2(A): B = A.astype(complex) for i, row in enumerate(A): B[i,:] = fft(row) for j, col in enumerate(B.T): B[:,j] = fft(col) return B
def __init__(self, n): """Initialize a secret key.""" # Public parameters self.n = n self.sigma = Params[n]["sigma"] self.sigmin = Params[n]["sigmin"] self.signature_bound = Params[n]["sig_bound"] self.sig_bytelen = Params[n]["sig_bytelen"] # Compute NTRU polynomials f, g, F, G verifying fG - gF = q mod Phi self.f, self.g, self.F, self.G = ntru_gen(n) # From f, g, F, G, compute the basis B0 of a NTRU lattice # as well as its Gram matrix and their fft's. B0 = [[self.g, neg(self.f)], [self.G, neg(self.F)]] G0 = gram(B0) self.B0_fft = [[fft(elt) for elt in row] for row in B0] G0_fft = [[fft(elt) for elt in row] for row in G0] self.T_fft = ffldl_fft(G0_fft) # Normalize Falcon tree normalize_tree(self.T_fft, self.sigma) # The public key is a polynomial such that h*f = g mod (Phi,q) self.h = div_zq(self.g, self.f)
def __init__(self, d, m, q): """ Initialize a secret key. """ # Public parameters self.d = d self.m = m self.q = q self.hash_function = hashlib.shake_256 slack = 1.1 smooth = 1.28 while (1): try: # Key generation self.A, self.B, invB, self.sq_gs_norm = module_ntru_gen( d, q, m) G = gram(self.B) self.B_fft = [[fft(elt) for elt in row] for row in self.B] self.invB_fft = [[fft(elt) for elt in row] for row in invB] G_fft = [[fft(elt) for elt in row] for row in G] self.T_fft = ffldl_fft(G_fft) self.sigma = smooth * sqrt(self.sq_gs_norm) self.signature_bound = slack * (self.m + 1) * self.d * (self.sigma**2) normalize_tree(self.T_fft, self.sigma) self.rate = bitsize(int(round(self.sigma))) - 2 # print("self.rate =", self.rate) return except ValueError: continue
def test_sample_1(self): signal = [1, 2, 3, 4, 5, 6, 7, 8] signal = fft(signal) self.assertEqual(list(signal), [4, 8, 2, 2, 6, 1, 5, 8]) signal = fft(signal) self.assertEqual(list(signal), [3, 4, 0, 4, 0, 4, 3, 8]) signal = fft(signal) self.assertEqual(list(signal), [0, 3, 4, 1, 5, 5, 1, 8])
def test_sample_2(self): signal = [1, 2, 3, 4, 5, 6, 7, 8] signal = fft(signal) self.assertEqual(list(signal)[4:], [6, 1, 5, 8]) signal = fft(signal) self.assertEqual(list(signal)[4:], [0, 4, 3, 8]) signal = fft(signal) self.assertEqual(list(signal)[4:], [5, 5, 1, 8])
def conv_fft(y, z, n): y_value = fft(y) z_value = fft(z) result = [y_value[i] * z_value[i] / n for i in xrange(n)] return [i.real for i in ifft(result)]
def get_coord_in_fft(self, point): """Compute t such that t*B0 = c.""" c0, c1 = point [[a, b], [c, d]] = self.B0_fft c0_fft, c1_fft = fft(c0), fft(c1) t0_fft = [(c0_fft[i] * d[i] - c1_fft[i] * c[i]) / self.q for i in range(self.n)] t1_fft = [(-c0_fft[i] * b[i] + c1_fft[i] * a[i]) / self.q for i in range(self.n)] return t0_fft, t1_fft
def test_example_00(): input_sequence = '12345678' output_sequence_1 = fft.fft(input_sequence, 1) output_sequence_2 = fft.fft(input_sequence, 2) output_sequence_3 = fft.fft(input_sequence, 3) output_sequence_4 = fft.fft(input_sequence, 4) assert len(str(input_sequence)) == len(output_sequence_1) assert output_sequence_1 == '48226158' assert output_sequence_2 == '34040438' assert output_sequence_3 == '03415518' assert output_sequence_4 == '01029498'
def multiply_poly(coeffs1, coeffs2): deg1 = len(coeffs1) deg2 = len(coeffs2) coeffs1 += [0] * (deg2 + 1) coeffs2 += [0] * (deg1 + 1) pts1 = fft(coeffs1, False) pts2 = fft(coeffs2, False) ptsmult = [] for (pt_1, pt_2) in zip(pts1, pts2): ptsmult.append(pt_1 * pt_2) return [round_j(p / len(ptsmult), 10) for p in fft(ptsmult, True)]
def zero_polynomial_via_gcd(root_of_unity, zero_vector): e1 = [0 if x == 0 or i % 2 == 0 else 1 for i, x in enumerate(zero_vector)] e2 = [0 if x == 0 or i % 2 == 1 else 1 for i, x in enumerate(zero_vector)] p1 = fft(e1, primefield.modulus, root_of_unity, inv=True) p2 = fft(e2, primefield.modulus, root_of_unity, inv=True) zero_poly = primefield.fast_extended_euclidean_algorithm(p1, p2)[0] assert primefield.degree(zero_poly) == len( list(filter(lambda x: x == 0, zero_vector))) r = fft(zero_poly, primefield.modulus, root_of_unity) assert all(a == 0 and b == 0 or a != 0 and b != 0 for a, b in zip(zero_vector, r)) return r, zero_poly
def get_mfcc_feature(self, hadcropped=False): ''' calculate Mel-frequency cepstral coefficients in frequency domain and extract features from MFCC :return: numpy array ''' assert self.frame_per_second not in [32, 64, 128, 256], \ Exception("Cannot operate butterfly computation ," "frame per second should in [32, 64, 128, 256]") hanning_kernel = self.get_window(method='hanning') windowed = self._add_window(hanning_kernel, self.meta_audio_data) # [num_frame, kernel_size] hanning_energy = self.get_energy(self.meta_audio_data, hanning_kernel) if not hadcropped: boundary = self.get_boundary(hanning_energy) cropped = windowed[boundary[0]: boundary[1] + 1, :] frequency = np.vstack([fft.fft(frame.squeeze()) for frame in np.vsplit(cropped, len(cropped))]) else: frequency = np.vstack([fft.fft(windowed)]) frequency = np.abs(frequency) frequency_energy = frequency ** 2 low_freq = self.sr / self.num_per_frame high_freq = self.sr H = self._mfcc_filter(self.mfcc_cof, low_freq, high_freq) S = np.dot(frequency_energy, H.transpose()) # (F, M) cos_ary = self._discrete_cosine_transform() mfcc_raw_features = np.sqrt(2 / self.mfcc_cof) * np.dot(S, cos_ary) # (F,N) upper = [self.get_upper_rate(fea) for fea in mfcc_raw_features.transpose()] assert len(upper) == mfcc_raw_features.shape[1] func = lambda x: [ # feature_calc.autocorrelation(norm(x), 5), np.std(x), feature_calc.approximate_entropy(norm(x), 5, 1), feature_calc.cid_ce(x, normalize=True), feature_calc.count_above_mean(x), feature_calc.first_location_of_minimum(x), feature_calc.first_location_of_maximum(x), feature_calc.last_location_of_maximum(x), feature_calc.last_location_of_minimum(x), feature_calc.longest_strike_above_mean(x), feature_calc.number_crossing_m(x, 0.8*np.max(x)), feature_calc.skewness(x), feature_calc.time_reversal_asymmetry_statistic(x, 5) ] mfcc_features = np.hstack( [func(col) for col in mfcc_raw_features.transpose()] ) return mfcc_features
def reduce(f, g, F, G): """ Reduce (F, G) relatively to (f, g). This is done via Babai's reduction. (F, G) <-- (F, G) - k * (f, g), where k = round((F f* + G g*) / (f f* + g g*)). Corresponds to algorithm 7 (Reduce) of Falcon's documentation. """ n = len(f) size = max(53, bitsize(min(f)), bitsize(max(f)), bitsize(min(g)), bitsize(max(g))) f_adjust = [elt >> (size - 53) for elt in f] g_adjust = [elt >> (size - 53) for elt in g] fa_fft = fft(f_adjust) ga_fft = fft(g_adjust) while (1): # Because we work in finite precision to reduce very large polynomials, # we may need to perform the reduction several times. Size = max(53, bitsize(min(F)), bitsize(max(F)), bitsize(min(G)), bitsize(max(G))) if Size < size: break F_adjust = [elt >> (Size - 53) for elt in F] G_adjust = [elt >> (Size - 53) for elt in G] Fa_fft = fft(F_adjust) Ga_fft = fft(G_adjust) den_fft = add_fft(mul_fft(fa_fft, adj_fft(fa_fft)), mul_fft(ga_fft, adj_fft(ga_fft))) num_fft = add_fft(mul_fft(Fa_fft, adj_fft(fa_fft)), mul_fft(Ga_fft, adj_fft(ga_fft))) k_fft = div_fft(num_fft, den_fft) k = ifft(k_fft) k = [int(round(elt)) for elt in k] if all(elt == 0 for elt in k): break # The two next lines are the costliest operations in ntru_gen # (more than 75% of the total cost in dimension n = 1024). # There are at least two ways to make them faster: # - replace Karatsuba with Toom-Cook # - mutualized Karatsuba, see ia.cr/2020/268 # For simplicity reasons, we didn't implement these optimisations here. fk = karamul(f, k) gk = karamul(g, k) for i in range(n): F[i] -= fk[i] << (Size - size) G[i] -= gk[i] << (Size - size) return F, G
def reduce(f, g, F, G): """ Reduce (F, G) relatively to (f, g). This is done via Babai's reduction. (F, G) <-- (F, G) - k * (f, g), where k = round((F f* + G g*) / (f f* + g g*)). Similar to algorithm Reduce of Falcon's documentation. Input: f, g, F, G Four polynomials mod (x ** n + 1) Output: None The inputs are reduced as detailed above. Format: Coefficient """ n = len(f) size = max(53, bitsize(min(f)), bitsize(max(f)), bitsize(min(g)), bitsize(max(g))) f_adjust = [elt >> (size - 53) for elt in f] g_adjust = [elt >> (size - 53) for elt in g] fa_fft = fft(f_adjust) ga_fft = fft(g_adjust) while(1): # Because we are working in finite precision to reduce very large polynomials, # we may need to perform the reduction several times. Size = max(53, bitsize(min(F)), bitsize(max(F)), bitsize(min(G)), bitsize(max(G))) if Size < size: break F_adjust = [elt >> (Size - 53) for elt in F] G_adjust = [elt >> (Size - 53) for elt in G] Fa_fft = fft(F_adjust) Ga_fft = fft(G_adjust) den_fft = add_fft(mul_fft(fa_fft, adj_fft(fa_fft)), mul_fft(ga_fft, adj_fft(ga_fft))) num_fft = add_fft(mul_fft(Fa_fft, adj_fft(fa_fft)), mul_fft(Ga_fft, adj_fft(ga_fft))) k_fft = div_fft(num_fft, den_fft) k = ifft(k_fft) k = [int(round(elt)) for elt in k] if all(elt == 0 for elt in k): break fk = karamul(f, k) gk = karamul(g, k) for i in range(n): F[i] -= fk[i] << (Size - size) G[i] -= gk[i] << (Size - size) return F, G
def getFFTMatrix(input, out, mp4, fftsize): file_replay = Path(out) if file_replay.is_file() == False and mp4 == True: command = "ffmpeg -i" + input + "-c copy -map 0:a audio.wav" ### subprocess.call(command, shell=True) audio = wave.open(out, 'rb') fmt = {1: 'B', 2: 'h', 4: 'i'} size = fmt[audio.getsampwidth()] a = array.array(size) a.fromfile(open(out, 'rb'), int(os.path.getsize(out) / a.itemsize)) bytes = a.tolist() audio.close() complexNums = list(map(cmplx.Complex, bytes)) mat = [] prev = [] ifft = [] for i in range(1, len(complexNums) // fftsize): start = (i - 1) * fftsize stop = i * fftsize mat.append(fft.fft(complexNums[start:stop])) return mat
def zero_polynomial_via_multiplication(root_of_unity, zero_vector): reduction_factor = 4 ps = [[1]] for i, x in enumerate(zero_vector): if x == 0: if primefield.degree(ps[-1]) < 63: ps[-1] = primefield.mul_polys( ps[-1], [-pow(root_of_unity, i, primefield.modulus), 1]) else: ps.append([-pow(root_of_unity, i, primefield.modulus), 1]) while len(ps) > 1: if len(ps) < 2 * reduction_factor and len(ps) > reduction_factor: psnew = [ primefield.mul_many_polys(ps[:len(ps) // 2]), primefield.mul_many_polys(ps[len(ps) // 2:]) ] else: psnew = [ primefield.mul_many_polys(l) for l in zip( * [ps[i::reduction_factor] for i in range(reduction_factor)]) ] if len(ps) % reduction_factor != 0: psnew.append( primefield.mul_many_polys( ps[-(len(ps) % reduction_factor):])) ps = psnew zero_poly = ps[0] assert primefield.degree(zero_poly) == len( list(filter(lambda x: x == 0, zero_vector))) r = fft(zero_poly, primefield.modulus, root_of_unity) assert all(a == 0 and b == 0 or a != 0 and b != 0 for a, b in zip(zero_vector, r)) return r, zero_poly
def get_polynomial(data): """ Interpolate a polynomial (coefficients) from data in reverse bit order """ assert is_power_of_two(len(data)) root_of_unity = get_root_of_unity(len(data)) return fft(list_to_reverse_bit_order(data), MODULUS, root_of_unity, True)
def get_data(polynomial): """ Get data (in reverse bit order) from polynomial in coefficient form """ assert is_power_of_two(len(polynomial)) root_of_unity = get_root_of_unity(len(polynomial)) return list_to_reverse_bit_order(fft(polynomial, MODULUS, root_of_unity, False))
def test_fri(): # Pure FRI tests poly = list(range(4096)) root_of_unity = pow(7, (modulus - 1) // 16384, modulus) evaluations = fft(poly, modulus, root_of_unity) proof = prove_low_degree(evaluations, root_of_unity, 4096, modulus) print("Approx proof length: %d" % bin_length(compress_fri(proof))) assert verify_low_degree_proof( merkelize(evaluations)[1], root_of_unity, proof, 4096, modulus) try: fakedata = [ x if pow(3, i, 4096) > 400 else 39 for x, i in enumerate(evaluations) ] proof2 = prove_low_degree(fakedata, root_of_unity, 4096, modulus) assert verify_low_degree_proof( merkelize(fakedata)[1], root_of_unity, proof, 4096, modulus) raise Exception("Fake data passed FRI") except: pass try: assert verify_low_degree_proof( merkelize(evaluations)[1], root_of_unity, proof, 2048, modulus) raise Exception("Fake data passed FRI") except: pass
def POST(self): web.header('Access-Control-Allow-Origin', 'http://49.233.200.100:4001') web.header('content-type', 'json') file = web.input().get('file', '') high_frequency = int(web.input().get('high_frequency', '')) low_frequency = int(web.input().get('low_frequency', '')) if file == '' or high_frequency == '' or low_frequency == '': return print(file[0:100]) file = file.split(',', maxsplit=1)[1] img = toBase64.to_image(file) fft_img = toBase64.to_base64(fft.fft(img)) high_pass_img = toBase64.to_base64(highPassFilter.high_pass_filter(img, high_frequency)) low_pass_img = toBase64.to_base64(lowPassFilter.low_pass_filter(img, low_frequency)) data = { 'fft': fft_img, 'high_pass': high_pass_img, 'low_pass': low_pass_img } return json.dumps(data)
def fk20_single_data_availability_optimized(polynomial, setup): """ Special version of the FK20 for the situation of data availability checks: The upper half of the polynomial coefficients is always 0, so we do not need to extend to twice the size for Toeplitz matrix multiplication """ assert is_power_of_two(len(polynomial)) n = len(polynomial) // 2 assert all(x == 0 for x in polynomial[n:]) reduced_polynomial = polynomial[:n] # Preprocessing part -- this is independent from the polynomial coefficients and can be # done before the polynomial is known, it only needs to be computed once x = setup[0][n - 2::-1] + [b.Z1] xext_fft = toeplitz_part1(x) toeplitz_coefficients = reduced_polynomial[-1::] + [0] * ( n + 1) + reduced_polynomial[1:-1] # Compute the vector h from the paper using a Toeplitz matric multiplication h = toeplitz_part3(toeplitz_part2(toeplitz_coefficients, xext_fft)) h = h + [b.Z1] * n # The proofs are the DFT of the h vector return fft(h, MODULUS, get_root_of_unity(2 * n))
def sample_preimage_fft(self, point): """ Sample preimage. Input: self The private key point An element of Z_q[x] / (x ** d + 1) Output: s A short element such that s * B = point Format: Coefficient """ d = self.d m = self.m # Compute large preimage c = [point] + [[0] * d for _ in range(m)] # Move to FFT domain c_fft = [fft(elt) for elt in c] # Compute coefficients in span(B) t_fft = vecmatmul_fft(c_fft, self.invB_fft) # Fast Fourier sampling z_fft = ffsampling_fft(t_fft, self.T_fft) # Compute short preimage s = (c - v) = (t - z) * B v_fft = vecmatmul_fft(z_fft, self.B_fft) v = [[int(round(coef)) for coef in ifft(elt)] for elt in v_fft] s = [sub(c[i], v[i]) for i in range(m + 1)] return s
def sample_preimage(self, point): """ Sample a short vector s such that s[0] + s[1] * h = point. """ [[a, b], [c, d]] = self.B0_fft # We compute a vector t_fft such that: # (fft(point), fft(0)) * B0_fft = t_fft # Because fft(0) = 0 and the inverse of B has a very specific form, # we can do several optimizations. point_fft = fft(point) t0_fft = [(point_fft[i] * d[i]) / q for i in range(self.n)] t1_fft = [(-point_fft[i] * b[i]) / q for i in range(self.n)] t_fft = [t0_fft, t1_fft] # We now compute v such that: # v = z * B0 for an integral vector z # v is close to (point, 0) z_fft = ffsampling_fft(t_fft, self.T_fft, self.sigmin) v0_fft = add_fft(mul_fft(z_fft[0], a), mul_fft(z_fft[1], c)) v1_fft = add_fft(mul_fft(z_fft[0], b), mul_fft(z_fft[1], d)) v0 = [int(round(elt)) for elt in ifft(v0_fft)] v1 = [int(round(elt)) for elt in ifft(v1_fft)] # The difference s = (point, 0) - v is such that: # s is short # s[0] + s[1] * h = point s = [sub(point, v0), neg(v1)] return s
def check_proof_multi(commitment, proof, x, ys, setup): """ Check a proof for a Kate commitment for an evaluation f(x w^i) = y_i """ n = len(ys) root_of_unity = get_root_of_unity(n) # Interpolate at a coset. Note because it is a coset, not the subgroup, we have to multiply the # polynomial coefficients by x^i interpolation_polynomial = fft(ys, MODULUS, root_of_unity, True) interpolation_polynomial = [div(c, pow(x, i, MODULUS)) for i, c in enumerate(interpolation_polynomial)] # Verify the pairing equation # # e([commitment - interpolation_polynomial(s)], [1]) = e([proof], [s^n - x^n]) # equivalent to # e([commitment - interpolation_polynomial]^(-1), [1]) * e([proof], [s^n - x^n]) = 1_T # xn_minus_yn = b.add(setup[1][n], b.multiply(b.neg(b.G2), pow(x, n, MODULUS))) commitment_minus_interpolation = b.add(commitment, b.neg(lincomb( setup[0][:len(interpolation_polynomial)], interpolation_polynomial, b.add, b.Z1))) pairing_check = b.pairing(b.G2, b.neg(commitment_minus_interpolation), False) pairing_check *= b.pairing(xn_minus_yn, proof, False) pairing = b.final_exponentiate(pairing_check) return pairing == b.FQ12.one()
def getFFTMatrix(input, out, mp4, fftsize): file_replay = Path(out) if file_replay.is_file() == False and mp4 == True: command = "ffmpeg -i" + input + "-c copy -map 0:a audio.wav" subprocess.call(command, shell=True) audio = wave.open(out, 'rb') fmt = {1: 'B', 2: 'h', 4: 'i'} size = fmt[audio.getsampwidth()] a = array.array(size) a.fromfile(open(out, 'rb'), os.path.getsize(out) / a.itemsize) bytes = a.tolist() audio.close() complexNums = map(cmplx.Complex, bytes) print(len(complexNums)) print("Samples: " + str(len(bytes))) mat = [] prev = [] ifft = [] print(len(mat)) # for i in range(0, len(complexNums)): # multiplier = 0.5 * (1 - np.cos(2 * np.pi * i / (fftsize - 1))) # complexNums[i] = complexNums)[i].scale(multiplier) for i in range(1, len(complexNums) / fftsize): start = (i - 1) * fftsize stop = i * fftsize mat.append(fft.fft(complexNums[start:stop])) #prev.append(complexNums[start:stop]) # if mp4 == False: # fft.matrixToCSV(prev, "./dedede512.csv") return mat
def verify_mimc_proof(inp, steps, round_constants, output, proof): p_root, d_root, b_root, l_root, branches, fri_proof = proof start_time = time.time() assert steps <= 2**32 // extension_factor assert is_a_power_of_2(steps) and is_a_power_of_2(len(round_constants)) assert len(round_constants) < steps precision = steps * extension_factor # Get (steps)th root of unity G2 = f.exp(7, (modulus-1)//precision) skips = precision // steps # Gets the polynomial representing the round constants skips2 = steps // len(round_constants) constants_mini_polynomial = fft(round_constants, modulus, f.exp(G2, extension_factor * skips2), inv=True) # Verifies the low-degree proofs assert verify_low_degree_proof(l_root, G2, fri_proof, steps * 2, modulus, exclude_multiples_of=extension_factor) # Performs the spot checks k1 = int.from_bytes(blake(p_root + d_root + b_root + b'\x01'), 'big') k2 = int.from_bytes(blake(p_root + d_root + b_root + b'\x02'), 'big') k3 = int.from_bytes(blake(p_root + d_root + b_root + b'\x03'), 'big') k4 = int.from_bytes(blake(p_root + d_root + b_root + b'\x04'), 'big') samples = spot_check_security_factor positions = get_pseudorandom_indices(l_root, precision, samples, exclude_multiples_of=extension_factor) last_step_position = f.exp(G2, (steps - 1) * skips) for i, pos in enumerate(positions): x = f.exp(G2, pos) x_to_the_steps = f.exp(x, steps) p_of_x = verify_branch(p_root, pos, branches[i*5]) p_of_g1x = verify_branch(p_root, (pos+skips)%precision, branches[i*5 + 1]) d_of_x = verify_branch(d_root, pos, branches[i*5 + 2]) b_of_x = verify_branch(b_root, pos, branches[i*5 + 3]) l_of_x = verify_branch(l_root, pos, branches[i*5 + 4]) zvalue = f.div(f.exp(x, steps) - 1, x - last_step_position) k_of_x = f.eval_poly_at(constants_mini_polynomial, f.exp(x, skips2)) # Check transition constraints C(P(x)) = Z(x) * D(x) assert (p_of_g1x - p_of_x ** 3 - k_of_x - zvalue * d_of_x) % modulus == 0 # Check boundary constraints B(x) * Q(x) + I(x) = P(x) interpolant = f.lagrange_interp_2([1, last_step_position], [inp, output]) zeropoly2 = f.mul_polys([-1, 1], [-last_step_position, 1]) assert (p_of_x - b_of_x * f.eval_poly_at(zeropoly2, x) - f.eval_poly_at(interpolant, x)) % modulus == 0 # Check correctness of the linear combination assert (l_of_x - d_of_x - k1 * p_of_x - k2 * p_of_x * x_to_the_steps - k3 * b_of_x - k4 * b_of_x * x_to_the_steps) % modulus == 0 print('Verified %d consistency checks' % spot_check_security_factor) print('Verified STARK in %.4f sec' % (time.time() - start_time)) return True
def generate_setup(size, secret): """ Generates a setup in the G1 group and G2 group, as well as the Lagrange polynomials in G1 (via FFT) """ g1_setup = [blst.G1().mult(pow(secret, i, MODULUS)) for i in range(size)] g2_setup = [blst.G2().mult(pow(secret, i, MODULUS)) for i in range(size)] g1_lagrange = fft(g1_setup, MODULUS, ROOT_OF_UNITY, inv=True) return {"g1": g1_setup, "g2": g2_setup, "g1_lagrange": g1_lagrange}
def test1(self): message ='03036732577212944063491565474664' offset = message[:7] self.assertEqual(offset, '0303673') new_message = fft(message) print(new_message) start = int(offset) % len(new_message) self.assertEqual(new_message[start: start+8], '84462026')
def mul_many_polys(self, ps, result_in_evaluation_form=False, size=0): if result_in_evaluation_form: n = size else: n = next_power_of_two(sum(self.degree(p) for p in ps) + 1) if (self.modulus - 1) % n == 0: root_of_unity = pow(self.primitive_root, (self.modulus - 1) // n, self.modulus) ps_fft = [fft(p, self.modulus, root_of_unity) for p in ps] r = [prod(l) for l in zip(*ps_fft)] if result_in_evaluation_form: return r return self.truncate_poly( fft(r, self.modulus, root_of_unity, inv=True)) else: assert not result_in_evaluation_form return reduce(lambda a, b: super().mul_polys(a, b), ps)
def get_extended_data(polynomial): """ Get extended data (expanded by 2x, reverse bit order) from polynomial in coefficient form """ assert is_power_of_two(len(polynomial)) extended_polynomial = polynomial + [0] * len(polynomial) root_of_unity = get_root_of_unity(len(extended_polynomial)) return list_to_reverse_bit_order(fft(extended_polynomial, MODULUS, root_of_unity, False))
def fftFromLiteMap(liteMap,applySlepianTaper = False,nresForSlepian=3.0,fftType=None): """ @brief Creates an fft2D object out of a liteMap @param liteMap The map whose fft is being taken @param applySlepianTaper If True applies the lowest order taper (to minimize edge-leakage) @param nresForSlepian If above is True, specifies the resolution of the taeper to use. """ ft = fft2D() ft.Nx = liteMap.Nx ft.Ny = liteMap.Ny flTrace.issue("flipper.fftTools",1, "Taking FFT of map with (Ny,Nx)= (%f,%f)"%(ft.Ny,ft.Nx)) ft.pixScaleX = liteMap.pixScaleX ft.pixScaleY = liteMap.pixScaleY lx = 2*numpy.pi * fftfreq( ft.Nx, d = ft.pixScaleX ) ly = 2*numpy.pi * fftfreq( ft.Ny, d = ft.pixScaleY ) ix = numpy.mod(numpy.arange(ft.Nx*ft.Ny),ft.Nx) iy = numpy.arange(ft.Nx*ft.Ny)/ft.Nx modLMap = numpy.zeros([ft.Ny,ft.Nx]) modLMap[iy,ix] = numpy.sqrt(lx[ix]**2 + ly[iy]**2) ft.modLMap = modLMap ft.lx = lx ft.ly = ly ft.ix = ix ft.iy = iy ft.thetaMap = numpy.zeros([ft.Ny,ft.Nx]) ft.thetaMap[iy[:],ix[:]] = numpy.arctan2(ly[iy[:]],lx[ix[:]]) ft.thetaMap *=180./numpy.pi map = liteMap.data.copy() #map = map0.copy() #map[:,:] =map0[::-1,:] taper = map.copy()*0.0 + 1.0 if (applySlepianTaper) : try: f = open(taperDir + os.path.sep + 'taper_Ny%d_Nx%d_Nres%3.1f'%(ft.Ny,ft.Nx,nresForSlepian)) taper = pickle.load(f) f.close() except: taper = slepianTaper00(ft.Nx,ft.Ny,nresForSlepian) f = open(taperDir + os.path.sep + 'taper_Ny%d_Nx%d_Nres%3.1f'%(ft.Ny,ft.Nx,nresForSlepian),mode="w") pickle.dump(taper,f) f.close() if fftType=='fftw3': ft.kMap = fft.fft(map*taper,axes=[-2,-1]) else: ft.kMap = fft2(map*taper) del map, modLMap, lx, ly return ft
def reconstruct_polynomial_from_samples(root_of_unity, samples, zero_polynomial_function): zero_vector = [0 if x is None else 1 for x in samples] time_a = time() zero_eval, zero_poly = zero_polynomial_function(root_of_unity, zero_vector) time_b = time() poly_evaluations_with_zero = [(0 if x is None else x) * y for x, y in zip(samples, zero_eval)] poly_with_zero = fft(poly_evaluations_with_zero, MODULUS, root_of_unity, inv=True) shift_factor = PRIMITIVE_ROOT_OF_UNITY shift_inv = primefield.inv(PRIMITIVE_ROOT_OF_UNITY) shifted_poly_with_zero = shift_poly(poly_with_zero, MODULUS, PRIMITIVE_ROOT_OF_UNITY) shifted_zero_poly = shift_poly(zero_poly, MODULUS, PRIMITIVE_ROOT_OF_UNITY) eval_shifted_poly_with_zero = fft(shifted_poly_with_zero, MODULUS, ROOT_OF_UNITY) eval_shifted_zero_poly = fft(shifted_zero_poly, MODULUS, ROOT_OF_UNITY) eval_shifted_reconstructed_poly = [ primefield.div(a, b) for a, b in zip(eval_shifted_poly_with_zero, eval_shifted_zero_poly) ] shifted_reconstructed_poly = fft(eval_shifted_reconstructed_poly, MODULUS, ROOT_OF_UNITY, inv=True) reconstructed_poly = shift_poly(shifted_reconstructed_poly, MODULUS, shift_inv) reconstructed_data = fft(reconstructed_poly, MODULUS, ROOT_OF_UNITY) assert all(x is None or x == y for x, y in zip(samples, reconstructed_data)) return reconstructed_data, time_b - time_a
def fft2(a): ny = len(a); if ny == 0: return a nx = len(a[0]); if nx == 0: return a for y in range(ny): assert len(a[y]) == nx b = [None] * ny for y in range(ny): b[y] = fft.fft(a[y]) b = transpose(b) for x in range(nx): b[x] = fft.fft(b[x]) b = transpose(b) return b
def test_shuffle(self): fin = [(0, 0), (1, 0), (1, 0), (1, 0), (1, 0), (0, 0)] inverse = 0 fout_exp = [ (4.0, 0.0), (-1.5, -0.8660254037844388), (-0.5, -0.8660254037844386), (0.0, 0.0), (-0.5, 0.8660254037844388), (-1.5, 0.8660254037844386) ] fout_act = fft.fft(fin, inverse) good_result = self._check_result(fout_exp, fout_act) self.assertTrue(good_result, '%s vs %s' % (fout_exp, fout_act))
def correlation(A,B): fft.fft(A,1.) fft.fft(B,-1.) N = len(A) >> 1 corr = np.zeros(2*N) for i in range(N): corr[2*i] = A[2*i]*B[2*i] - A[2*i+1]*B[2*i+1] corr[2*i+1] = A[2*i+1]*B[2*i] + A[2*i]*B[2*i+1] fft.fft(corr,-1.) return corr
def computeCMBY(d0): """ For CMB, y = S^1/2 A N^-1 d, where S is CMB signal covariance matrix (Cl's) """ # N.B. Reshaping operations required to go between 2D pixel arrays and # 1D vector (for linear system) d2 = 0 for freq in range(nFreq): d1 = d0[freq].data.copy().reshape((ny,nx)) d1 *= ninvs[freq] a_l = fft.fft(d1,axes=[-2,-1]) a_l *= beams[freq]*precond_2d d1 = numpy.real(fft.ifft(a_l,axes=[-2,-1],normalize=True)) d1 = numpy.reshape(d1,(nx*ny)) d2 += d1 return d2
def correlation(A, B): fft.fft(A, 1.0) fft.fft(B, 1.0) N = len(A) >> 1 corr = np.zeros(2 * N) for i in range(2 * N): B[i] = -B[i] for j in range(N): corr[2 * j] = A[2 * j] * B[2 * j] - A[2 * j + 1] * B[2 * j + 1] corr[2 * j + 1] = A[2 * j + 1] * B[2 * j] + A[2 * j] * B[2 * j + 1] fft.fft(corr, -1.0) return corr
L = 2.*PI dx = L / (N-1) x = np.zeros(N) f = np.zeros(N) f_04 = np.zeros(2*N) f_13 = np.zeros(2*N) f_28 = np.zeros(2*N) f_39 = np.zeros(2*N) f_50 = np.zeros(2*N) for i in range(N): x[i] = i*dx f[i] = i for i in range(N): f_04[2*i] = np.sin(x[i]) + np.sin(4.*x[i]) f_13[2*i] = np.sin(x[i]) + np.sin(13.*x[i]) f_28[2*i] = np.sin(x[i]) + np.sin(28.*x[i]) f_39[2*i] = np.sin(x[i]) + np.sin(39.*x[i]) f_50[2*i] = np.sin(x[i]) + np.sin(50.*x[i]) fft.fft(f_04,1.) fft.fft(f_13,1.) fft.fft(f_28,1.) fft.fft(f_39,1.) fft.fft(f_50,1.) fft.plot_i_n([f[:0.5*N],f[:0.5*N],f[:0.5*N],f[:0.5*N],f[:0.5*N]], [f_04[:N],f_13[:N],f_28[:N],f_39[:N],f_50[:N]],["4","13","28","39","50"]) plt.show()
N = len(yinput) print 'Padded : ' print len(yinput) padlength=len(pads) # Apply a window to reduce ringing from the 2^n cutoff if window : for iy in xrange(len(yinput)) : yinput[iy] = yinput[iy] * (0.5 - 0.5 * math.cos(2*math.pi*iy/float(N-1))) y = array( yinput ) x = array([ float(i) for i in xrange(len(y)) ] ) Y = fft(y) maxfreq = 200 # Now smooth the data for iY in range(maxfreq, len(Y)-maxfreq ) : Y[iY] = complex(0,0) #Y[iY] = Y[iY] * (0.5 - 0.5 * math.cos(2*math.pi*iY/float(N-1))) #for iY in range(0,N) : # Y[iY] = Y[iY] * math.exp(-1.0*iY / 50.0) powery = fft_power(Y) powerx = array([ float(i) for i in xrange(len(powery)) ] ) Yre = [math.sqrt(Y[i].real**2+Y[i].imag**2) for i in xrange(len(Y))]
def conv(a, b): assert len(a) == len(b) n = len(a) a, b = fft(a), fft(b) return ifft(list(a[i] * b[i] / n for i in range(len(a))))
p2dWeight_TT = numpy.load('noiseAndWeights/weightMap_%s_%s_TT.npy'%(arrayTag,spTag)) p2dWeight_Cross = numpy.load('noiseAndWeights/weightMap_%s_%s_Cross.npy'%(arrayTag,spTag)) p2dWeight_Pol = numpy.load('noiseAndWeights/weightMap_%s_%s_Pol.npy'%(arrayTag,spTag)) p2dWeightArray={} p2dWeightArray['TT'],p2dWeightArray['EE'],p2dWeightArray['TE'],p2dWeightArray['ET']=p2dWeight_TT,p2dWeight_Pol,p2dWeight_Cross,p2dWeight_Cross p2dWeightArray['TB'],p2dWeightArray['EB'],p2dWeightArray['BT'],p2dWeightArray['BE']=p2dWeight_Cross,p2dWeight_Pol,p2dWeight_Cross,p2dWeight_Pol p2dWeightArray['BB']=p2dWeight_Pol window_T = liteMap.liteMapFromFits('auxMaps/finalWindow_%s_%s_T.fits'%(arrayTag,spTag)) window_Pol = liteMap.liteMapFromFits('auxMaps/finalWindow_%s_%s_Pol.fits'%(arrayTag,spTag)) fftTemp=window_T.copy() fft.fft(fftTemp.data,axes=[-2,-1],flags=['FFTW_MEASURE']) del fftTemp modLMap,angLMap=fftPol.makeEllandAngCoordinate(window_T,bufferFactor=1) p2dTemp=fftTools.powerFromLiteMap(window_T) p2dTemp.powerMap[:]=0 p2dTemp=p2dTemp.trimAtL(trimAtL+500) clAutoPatch = {} clCrossPatch = {} Sump2dPatch = {}
#! /usr/bin/python # # Test the fft and dft modules import fft import dft import math SIZE = 64 TWO_PI = 2.0*math.pi SCALE = TWO_PI/SIZE X = x = [0] * SIZE for i in range(0,SIZE): x[i] = complex( math.cos(4.0*i*SCALE), math.cos(6.0*i*SCALE) ) print "%d:%5.2f+%5.2fj" % ( i,x[i].real, x[i].imag ) X = fft.fft(x) print "FFT" for i in range(0,SIZE): print"%d:%5.2f+%5.2fj" % ( i,X[i].real,X[i].imag ) print "DFT" X = dft.dft(x) for i in range(0,SIZE): print "%d:%5.2f+%5.2fj" % ( i,X[i].real,X[i].imag )
def run_analysis(save=False, check=False, debug=False, verbose=False, movie=False, pdf=False, elog=False, filename=None): # ====================================== # User selects file # ====================================== if filename is None: data = E200.E200_load_data_gui() else: data = E200.E200_load_data(filename) loadname = os.path.splitext(os.path.basename(data.filename))[0] # ====================================== # User decides whether to view view # selected regions # ====================================== reply = mtqt.ButtonMsg(title='Show full analysis?', buttons=['Yes', 'No'], maintext='Show individual images analyzed? (MUCH slower)') if reply.clickeditem == 'Yes': check = True elif reply.clickeditem == 'No': check = False # ====================================== # Prep save folder # ====================================== savedir = 'output' if not os.path.isdir(savedir): os.makedirs(savedir) # ====================================== # Prep pdf # ====================================== if pdf or elog: if pdf: pdffilename = 'output.pdf' elif elog: tempdir_pdf = tempfile.TemporaryDirectory() pdffilename = os.path.join(tempdir_pdf.name, 'output.pdf') pngfilename = os.path.join(tempdir_pdf.name, 'out.png') pdfpgs = PdfPages(filename=pdffilename) # ====================================== # Load data # ====================================== # savefile = os.path.join(os.getcwd(), 'local.h5') # f = h5.File(savefile, 'r', driver='core', backing_store=False) # data = E200.Data(read_file = f) # ====================================== # Cameras to process # ====================================== camlist = ['AX_IMG1', 'AX_IMG2'] radii = [2, 1] calibrations = [10e-6, 17e-6] # ====================================== # UIDs in common # ====================================== uids = np.empty(2, dtype=object) for i, cam in enumerate(camlist): imgstr = getattr(data.rdrill.data.raw.images, cam) uids[i] = imgstr.UID uids_wanted = np.intersect1d(uids[0], uids[1]) uids_wanted = uids_wanted[uids_wanted > 1e5] num_uids = np.size(uids_wanted) # ====================================== # Process cameras # ====================================== blobs = np.empty(2, dtype=object) for i, (cam, radius, cal) in enumerate(zip(camlist, radii, calibrations)): imgstr = getattr(data.rdrill.data.raw.images, cam) blob = BlobAnalysis(imgstr, imgname=cam, cal=cal, reconstruct_radius=1, check=check, debug=debug, verbose=verbose, movie=movie, save=save, uids=uids_wanted) if save or check or pdf or elog: fig = blob.camera_figure(save=save, dataset=loadname) if pdf or elog: pdfpgs.savefig(fig) if check: # plt.show() plt.draw() plt.pause(0.0001) blobs[i] = blob # ====================================== # Process centroids into array of coords # correlated for 3d plotting # ====================================== z = np.array((0, 1.5)) coords = np.empty([0, num_uids, 3]) for i, blob in enumerate(blobs): z = i*1.5 * np.ones((np.size(blob.centroid, 0), 1)) # ipdb.set_trace() temp_cent = blob.centroid centered = temp_cent - np.mean(temp_cent, axis=0) coord = np.append(centered, z, axis=1) coords = np.append(coords, [coord], axis=0) # ====================================== # Plot relative 3D trajectories # ====================================== fig = plt.figure(figsize=(16, 6)) gs = gridspec.GridSpec(1, 2) ax1 = fig.add_subplot(gs[0, 0], projection='3d') ax2 = fig.add_subplot(gs[0, 1], projection='3d') # maxwidth = np.max(blobs[0].sigma_x * blobs[0].sigma_y) # widths = blobs[0].sigma_x[i]*blobs[0].sigma_y[i] / maxwidth for i, coord in enumerate(coords.swapaxes(0, 1)): ax1.plot(coord[:, 2], coord[:, 0]*1e6, coord[:, 1]*1e6) ax2.plot(coord[:, 2], coord[:, 0]*1e6, coord[:, 1]*1e6) mt.addlabel(ax=ax1, toplabel='Centroid Trajectory', xlabel='z [m]', ylabel='x [$\mu$m]', zlabel='y [$\mu$m]') mt.addlabel(ax=ax2, toplabel='Centroid Trajectory', xlabel='z [m]', ylabel='x [$\mu$m]', zlabel='y [$\mu$m]') fig.tight_layout() if pdf or elog: ax1.view_init(elev=45., azim=-60) ax2.view_init(elev=0., azim=0) mainfigtitle = '3D Plot' fig.suptitle(mainfigtitle, fontsize=22) fig.tight_layout(rect=[0, 0, 1, 0.95]) pdfpgs.savefig(fig) # ====================================== # Make a movie # ====================================== if movie: with tempfile.TemporaryDirectory() as tempdir: for ii in range(0, 360, 1): sys.stdout.write('\rFrame: {}'.format(ii)) ax1.view_init(elev=45., azim=ii-60) ax2.view_init(elev=0., azim=ii-60) fig.savefig(os.path.join(tempdir, 'movie_{:03d}.tif'.format(ii))) fileinput = os.path.join(tempdir, 'movie_%03d.tif') command = 'ffmpeg -y -framerate 30 -i {fileinput:} -vcodec h264 -r 30 -pix_fmt yuv420p {savedir:}/out.mov'.format(fileinput=fileinput, savedir=savedir) subprocess.call(shlex.split(command)) # ====================================== # Calculate angles # ====================================== dx = coords[0, :, 0] - coords[1, :, 0] dy = coords[0, :, 1] - coords[1, :, 1] ds = np.sqrt(dx**2 + dy**2) theta = np.arctan(ds/z.flat) theta_urad = theta * 1e6 # phi = np.arctan2(dy, dx) # phi = phi * (phi >= 0) + (phi < 0) * (-phi + 2*np.pi) # ====================================== # Plot joint analysis # ====================================== fig = plt.figure(figsize=(16, 12)) gs = gridspec.GridSpec(2, 2) ax1 = fig.add_subplot(gs[0, 0]) ax1.plot(theta_urad, '-o') mt.addlabel(ax=ax1, toplabel='Coordinate: $\\theta$', xlabel='Shot', ylabel='Angle Deviation from Average [$\mu$rad]') ax2 = fig.add_subplot(gs[1, 0]) mt.hist(theta_urad, bins=15, ax=ax2) mt.addlabel(ax=ax2, toplabel='Coordinate: $\\theta$', xlabel='Angle Deviation from Average [$\mu$rad]') ax3 = fig.add_subplot(gs[0, 1]) # ax3.plot(phi, '-o') n_color = np.size(dx) color = np.linspace(1, n_color, n_color) ax3_p = ax3.scatter(dx*1e6, dy*1e6, c=color, marker='o', cmap=plt.get_cmap('Greys')) cb = plt.colorbar(ax3_p) mt.addlabel(ax=ax3, xlabel='$\Delta x$ ($\mu$m)', ylabel = '$\Delta y$ ($\mu$m)', toplabel='Deviation from Straight-Ahead Average', cb=cb, clabel='Shot Number') ax4 = fig.add_subplot(gs[1, 1]) # mt.hist(phi, bins=15, ax=ax4) mt.hist2d(dx*1e6, dy*1e6, cmap=plt.get_cmap('Greys'), ax=ax4, interpolation='nearest') mt.addlabel(ax=ax4, xlabel='$\Delta x$ ($\mu$m)', ylabel = '$\Delta y$ ($\mu$m)', toplabel='Deviation from Straight-Ahead Average') mainfigtitle = 'Pointing Stability' fig.suptitle(mainfigtitle, fontsize=22) fig.tight_layout(rect=[0, 0, 1, 0.95]) # ====================================== # Save joint analysis # ====================================== if save: filename = os.path.join(savedir, 'PointingStability.png') fig.savefig(filename) # ====================================== # Save pdf # ====================================== if pdf or elog: # ====================================== # Save final fig # ====================================== pdfpgs.savefig(fig) freq=data.rdrill.data.raw.metadata.E200_state.EVNT_SYS1_1_BEAMRATE.dat[0] fig = fft(blobs, camlist, freq=freq) pdfpgs.savefig(fig) pdfpgs.close() if elog: mtim.pdf2png(file_in=pdffilename, file_out=pngfilename) author = 'E200 Python' title = 'Laser Stability: {}'.format(loadname) text = 'Laser stability analysis of AX_IMG and AX_IMG2. Comment: {}'.format(E200._numarray2str(data.rdrill.data.raw.metadata.param.comt_str)) file = pngfilename link = pdffilename mtft.print2elog(author=author, title=title, text=text, link=link, file=file) if not pdf: # ====================================== # Clean temp pdf directory # ====================================== tempdir_pdf.cleanup() # ====================================== # Show plots, debug # ====================================== if debug: plt.ion() plt.show() ipdb.set_trace() else: plt.show() return blobs
plotfirst = True if plotfirst == True : # make some fake data : N = 1024 f = 10 #spectral index vs x = array([ float(i) for i in xrange(N) ] ) y = array([ math.sin(-2*math.pi*f* xi / float(N)) for xi in x ]) #y = array([ xi for xi in x ]) Y = fft(y) Yre = [math.sqrt(Y[i].real**2 + Y[i].imag**2) for i in xrange(N)] yre=[Y[i].real for i in xrange(N)] yim=[Y[i].imag for i in xrange(N)] s1 = plt.subplot(4, 1, 1) plt.plot( x, y ) s2 = plt.subplot(4, 1, 2) s2.set_autoscalex_on(False) plt.plot( x, Yre ) plt.xlim([0,20]) plt.subplot(4,1,3) plt.plot(x,yre) plt.subplot(4,1,4) plt.plot(x,yim)
# The testing file for the dft function import math import fft import matplotlib.pyplot as pyp import time num=100 x=[i*1.0/num*4*math.pi for i in range(num)] fun=[math.sin(x[i])/1.6+math.cos(x[i]/3)+math.cos(x[i]*4)/3 for i in range(len(x))] sign=1 a=fft.fft(fun,sign) b=fft.invftt(a,sign) pyp.figure(1) pyp.title('The fast fourier transform F(x)') pyp.plot(x, fun,'b',label='$f(x)=sin(x)/6+cos(4x)/3+cos(x/3)$') pyp.plot(x, b,'r-.',label='$f(x)=F^{-1}(F(f(x)))$') pyp.legend() pyp.savefig('Trigonometric.png',format='png') num=1000 x=[i/1000.0 for i in range(num)]#[i./num for i in range(num)] fun=[0.25/((i-0.5)**2+0.25) for i in x] sign=1 a=fft.fft(fun,sign) b=fft.invftt(a,sign)
p = experiment.p cosmo = experiment.cosmo mapDir = experiment.mapDir nsims = p['nsims'] nsamp = p['nsamp'] # Load data and instrumental specifications template, power_2d, beams, ninvs, freqs = fist.experiment_settings(p) # Speed up FFT by measuring #t=time.time() fft.rfft(ninvs[0].copy(),axes=[-2,-1],flags=['FFTW_MEASURE']) template_l = fft.fft(ninvs[0],axes=[-2,-1]) fft.irfft(template_l,axes=[-2,-1],flags=['FFTW_MEASURE']) #Create a directory for the results of the chain resultDir = fist.check_dir_exists('results') for n in range(nsims): datamaps=[] count=0 for k in freqs: datamaps+=[fist.litemap_from_fits(mapDir+'/data_%03d_%d.fits'%(n,k))] mask=fist.litemap_from_fits(mapDir+'/mask_%d.fits'%(k))
import sys import numpy as np from matplotlib import pyplot as plt import fft N = 8 * 2 X = np.arange(N) V = np.cos(2*np.pi*X/N+0.5) + np.sin(2*np.pi*0.5*X/N) - 0.3*np.cos(2*np.pi*X/N) + 5.0*np.sin(2*np.pi*0.3*X/N) print 'V:', V Q1 = fft.fft(V) P1 = fft.ifft(Q1) plt.plot(X, V, '-b', X, np.real(Q1), '-g', X, np.real(P1), '-r') plt.show() print 'Q1:', Q1 print 'P1:', np.real(P1) Q2 = np.fft.fft(V) P2 = np.fft.ifft(Q2) plt.plot(X, V, '-b', X, np.real(Q2/np.sqrt(N)), '-g', X, np.real(P2), '-r') plt.show() print 'Q2:', Q2 print 'P2:', P2 max_dev = np.max(np.abs(P1-P2))