#!/usr/bin/env python3 from cmath import exp,pi class Expr: def __init__(self,s): self.s=s def convert_to_Expr_if_int(self, n): if isinstance(n, int): return Expr(str(n)) if isinstance(n, Expr): return n raise AssertionError # unsupported type def __str__(self): return self.s def __add__(self, other): return Expr("(" + self.s + "+" + self.convert_to_Expr_if_int(other).s + ")") def __sub__(self, other): return Expr("(" + self.s + "-" + self.convert_to_Expr_if_int(other).s + ")") def __mul__(self, other): return Expr("(" + self.s + "*" + self.convert_to_Expr_if_int(other).s + ")") def __pow__(self, other): return Expr("(" + self.s + "**" + self.convert_to_Expr_if_int(other).s + ")") def FFT(X): n = len(X) # cast complex value to string, and then to Expr w = Expr(str(exp(-2*pi*1j/n))) if n > 1: X = FFT(X[::2]) + FFT(X[1::2]) for k in range(int(n/2)): xk = X[k] X[k] = xk + w**k*X[int(k+n/2)] X[int(k+n/2)] = xk - w**k*X[int(k+n/2)] return X input=[Expr("input_%d" % i) for i in range(8)] output=FFT(input) for i in range(len(output)): print (i, ":", output[i])