Using GNU Octave FFT Features

I play with octave fft functions and I cannot figure out how to scale their output: I use the following (very short) code to approximate the function:

function y = f(x) y = x .^ 2; endfunction; X=[-4096:4095]/64; Y = f(X); # plot(X, Y); F = fft(Y); S = [0:2047]/2048; function points = approximate(input, count) size = size(input)(2); fourier = [fft(input)(1:count) zeros(1, size-count)]; points = ifft(fourier); endfunction; Y = f(X); plot(X, Y, X, approximate(Y, 10)); 

Basically, what he does is take a function, calculate the interval image, fft-it, then save a few harmonics and, if the result. However, I get a graph that is vertically compressed (the vertical output scale is incorrect). Any ideas?

+6
fft gnuplot octave
source share
2 answers

You are probably mistaken. You remove all the "negative" frequencies in the code. You must maintain both positive and negative low frequencies. Here is the code in python and the result. The graph has the correct scale.

alt text http://files.droplr.com/files/35740123/XUl90.fft.png

The code:

 from __future__ import division from scipy.signal import fft, ifft import numpy as np def approximate(signal, cutoff): fourier = fft(signal) size = len(signal) # remove all frequencies except ground + offset positive, and offset negative: fourier[1+cutoff:-cutoff] = 0 return ifft(fourier) def quad(x): return x**2 from pylab import plot X = np.arange(-4096,4096)/64 Y = quad(X) plot(X,Y) plot(X,approximate(Y,3)) 
+3
source share

You throw away the second half of the conversion. The transformation is Hermitian symmetric to real inputs, and you must save these lines. Try the following:

 function points = approximate(inp, count) fourier = fft(inp); fourier((count+1):(length(fourier)-count+1)) = 0; points = real(ifft(fourier)); %# max(imag(ifft(fourier))) should be around eps(real(...)) endfunction; 

The inverse transform invariably has some tiny imaginary part due to a numerical computation error, hence the extraction of real .

Note that input and size are keywords in Octave; smoothing with their own variables is a good way to get really weird errors!

+4
source share

All Articles