Ok. I've found a way (to copy an fft object) : lfft = len(fftobj) fftobj_copy = FFT(lfft) for i in range(lfft): fftobj_copy[i]=fftobj[i]
If you see things which I could / should do another way in my code, feel free to post your suggestions! On Dec 18, 11:45 am, Maxim <maxim.courno...@gmail.com> wrote: > Hello! > > I am working with the builtin FFT Fast Fourier Transform of Sagemath, > and have coded (based on the work of P. Lutus here > :http://vps.arachnoid.com/sage/fourier.html) a function that takes an > fftobject to plot the spectrum of a function. > > My problem is that when I pass the fftobject, I don't know how to make > a local copy of this object inside the scope of my function. The > result if that after my first call of this function, the fftobject > gets modified (globally) when I run fftobject.fordward_transform() on > it. This is not what I want, because I want to reuse this same object > unchanged further in my code. > > I'm new at Python, so I do not know a lot about this. I have first > tried to to something as : > fftobject_copy = fftobject > But it would just create a pointer to fftobject, and still allow the > function to modify its content. I tried to use the list copy : > fft_object_copy = fftobject[:] > But it would loose it's FFT specific functions, such as in > "fftobject.forward_transform()". > > So, how can I make a local copy of an FFT object? An example of my > code is following. > Many thanks! > > ## My functions declaration. Notice the fft_unilateral(fftobj) > function, where I'm trying to make a local copy of > ## fftobj. > # magnitude of 2-component cartesian vector > def mag(x): > return sqrt(x[0]^2+x[1]^2) > > def nextpow2(i): > n = 2 > while n < i: n = n * 2 > return n > > def fft_unilateral(fftobj): > # fftobj_copy = fftobj[:] <== Trying to create a local copy of > fftobj here! > fftobj_copy = fftobj # <== fftobj_copy is not a copy but a > pointer to fftobj! > fftobj_copy.forward_transform() > lfft = len(fftobj_copy) > dt = 1.0/lfft > list = map(lambda x:(2*mag(x))*dt,fftobj_copy[:lfft/2]) > list[0]=list[0]/2 > return list > > # frequency unilateral domain plot > def fft_plot(fftobj,line_color='blue',labels= > ('Frequence','Amplitude')): > list = fft_unilateral(fftobj) > return list_plot > (list,rgbcolor=line_color,plotjoined=True,axes_labels=labels) > > # Power Spectrum Density plot > def psd_plot(fftobj,line_color='blue',labels= > ('Frequence','Amplitude')): > list = fft_unilateral(fftobj) > for i in range(len(list)): > list[i]=list[i]^2 > return list_plot > (list,rgbcolor=line_color,plotjoined=True,axes_labels=labels) > > ## Begin main program > # Define time domain function (AM without overmodulation here) > m(t)= cos(2*pi*100*t) > p(t)= cos(2*pi*1000*t) > AM1(t)=(1 + 0.5*m(t))*p(t) > > # Create > samples=4000 > lfft=nextpow2(samples) > # samples = sampling frequency > # Create fft object > fft_a=FFT(lfft) > # Fill fftobject with time domain data > for t in range(lfft): > fft_a[t] = AM1(t/lfft) > > ## Finally, ready to fire it up. > # If you have succeded to create a local copy in the fft_unilateral > function, then the two following PSDs are > # identical. If not, then they differ. > fft_plot(fft_a,line_color='blue',labels=('$f$ (Hz)','$|\Phi(f)|$')) > > # Copy to new cell > fft_plot(fft_a,line_color='blue',labels=('$f$ (Hz)','$|\Phi(f)|$')) -- To post to this group, send email to sage-support@googlegroups.com To unsubscribe from this group, send email to sage-support+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/sage-support URL: http://www.sagemath.org