%Read in the file with y the array of values, fs the sampling frequency,
%and bits the number of bits per sample (16)
[y,fs,bits]=wavread('scramble.wav');

%Perform Fast Fourier Transform of time-domain signal
Y=fft(y);
%The frequency domain of the signal is divided into four bands, and with
%negative values, this corresponds to eight bands with fft.
%
%CBDAADBC   ABCDDCBA
%12345678   42136875
band_size=length(Y)/8;
Y_rearranged=zeros(length(Y),1);

%Rearrange the bands
Y_rearranged(1:band_size)=Y(3*band_size+1:4*band_size);
Y_rearranged(band_size+1:2*band_size)=Y(band_size+1:2*band_size);
Y_rearranged(2*band_size+1:3*band_size)=Y(1:band_size);
Y_rearranged(3*band_size+1:4*band_size)=Y(2*band_size+1:3*band_size);
Y_rearranged(4*band_size+1:5*band_size)=Y(5*band_size+1:6*band_size);
Y_rearranged(5*band_size+1:6*band_size)=Y(7*band_size+1:8*band_size);
Y_rearranged(6*band_size+1:7*band_size)=Y(6*band_size+1:7*band_size);
Y_rearranged(7*band_size+1:8*band_size)=Y(4*band_size+1:5*band_size);

%Take inverse transform and write new wave file, then play it
y_rearranged=ifft(Y_rearranged);
wavwrite(real(y_rearranged),fs,'descrambled.wav');
wavplay(real(y_rearranged),fs);