You should not publish such scripts, this
is less than a q&d hack. Besides poor programming style it is simply incorrect.
b = length(a)/2
Sorry, this will fail for odd number of elements. Besides that, the PSD is not simply the square of abs(fft), see here