% RTG definition file for pram experiment
%
% Version 3.0
%
% Andrew Senior, Lancaster University, 2012-06-22 (a.senior@lancaster.ac.uk)

thead={'Channel board 1 background' 'Channel board 2 background'};

% parameters

n_ch=1; % number of channelboards in use
n_rxsamp=150; % length of Rx sample vector
n_txsamp=20; % length of Tx sample vector
%n_txsamp=0; % for testing using pram v2 data
n_samp=n_txsamp+n_rxsamp; % length of sample vector

delta_t=4e-6; % sampling period (s)
pulse_len=20e-6; % pulse length (s)
t_start=270e-6; % delay from pulse start to sampling start (s)
ipp=10e-3; % inter-pulse period (s)
fft_len=32; % length of FFT for background spectrum

f_samp=1e-3/delta_t; % sampling frequency (kHz)

% Re-shape and divide up the samples
S=reshape(d_raw,n_samp,[]);
n_pulse=size(S,2)/n_ch; % number of pulses in one dump

% Pulse-to-pulse spectrograms
% O- and X-mode for each channel board

npol=2; % number of polarisations

ax=getaxes(20,2,2);
f_scale=((0:n_pulse/npol-1)-floor(n_pulse/npol/2))/(n_pulse*ipp);
r_scale=(t_start-pulse_len/2+((1:n_rxsamp)-1)*delta_t)*1.5e8; % range to *centre* of pulse
modechars='OX';

for chboard=0:n_ch-1
  for pol=0:1
    X=fft(S(n_txsamp+1:end,(1:2:n_pulse)+pol+chboard*n_pulse),[],2);
    X=fftshift(X,2);
    X=X.*conj(X);
    ah=ax(1+pol+chboard*2);
    updateimage(20,ah,f_scale,r_scale/1e3,X);
    set(get(ah,'xlabel'),'string','Frequency (Hz)');
    set(get(ah,'ylabel'),'string','Range (km)');
    set(get(ah,'title'),'string', ...
        sprintf('Pulse-to-pulse (Channel %d %s-mode)', ...
                chboard+1,modechars(pol+1)));
  end
end

% Background spectra (from furthest ranges)
% One for each channel board

ax=getaxes(40,2,2);
f_scale=((0:fft_len-1)-floor(fft_len/2))/(fft_len*delta_t);

for chboard=0:n_ch-1
  X=fft(S(end-(fft_len-1):end,(1:n_pulse)+chboard*n_pulse));
  X=fftshift(X,1);
  X=X.*conj(X);
  X=sum(X,2);
  ah=ax(chboard+1);
  updateplot(40,ah,f_scale/1e3,10*log10(X),[],[]);
  set(get(ah,'xlabel'),'string','Frequency (kHz)');
  set(get(ah,'ylabel'),'string','Power (dB)');
  set(get(ah,'title'),'string', ...
      sprintf('Background Spectrum (Channel %d)',chboard+1));

end

