% this code does a one dimensional fourier transform and gives you the
% power spectrum

function [k_x,p_spec]=power_spec_1D(x,y)

% I assume that y is a function of x. In this code, we transform the x
% coordinate to the wave-space representation k_x. Doing so answers the
% question: "How much structure is there on the scale of 2*pi/k_x?"

%% make sure that there are an even number of data points

% As it happens this code is much simpler to write if x has an even number
% of data points. Specifically since y is real valued y(-k_x) is the
% complex conjugate of y(k_x) and so carries no information. Thus only the
% second half (k_x>0) is interesting. Furthermore, the FFT algorithm that
% matlab uses is much faster if the number of elements of x is a
% power of 2. All of our data sets are small enough that I am not going to
% worry about this
l_x=length(x);
if floor(l_x/2)~=l_x/2
    x(1)=[];
    y(1)=[];
    l_x=l_x-1;
end

%% do the fourier transform
FFT_y=fft(y);
p_spec=abs(FFT_y/(l_x/2)).^2; % this is the power spectrum

p_spec=p_spec(1:l_x/2); % throw out half the data

%% find k_x
delta_x=x(2)-x(1);
k_x=pi*linspace(0,1,l_x/2)/(delta_x);


