% 
% Read Couple flat binary complex pressure file and plot transmission
% loss (Richard B. Evans, 99F Hugo Rd. N. Stonington, CT 06359, 6/12/08)
%
clear
%
% Open binary data file
%
bin_data_file='\Models\cmodes\oalib_couple07\test2.cpr';
bfid = fopen(bin_data_file,'r','ieee-le');
%
% Read header
%
title = char(fread(bfid,80,'uchar'))'
da = char(fread(bfid,10,'uchar'))'
fsd = fread(bfid,2,'float32');
nrnd = fread(bfid,2,'int32');
% Number of ranges may be wrong (zero)
nr=nrnd(1);
% Number of reciever depths is ok
nd=nrnd(2)
% Frequency and source depth
freq = fsd(1)
src_depth = fsd(2)
% Receiver depths
rd = fread(bfid,nd,'float32');
%
% Read range and complex pressure to end. All the data is
% put in a single real array: rng_cpr_array
%
rng_cpr_array=fread(bfid,[1+2*nd, inf],'float32');
%
% Close binary data file
fclose(bfid);
%
% Find the nuber of ranges
%
nr=size(rng_cpr_array,2)
%
% Separate out the range and the real and imaginary parts of the
% complex pressure
%
rng=rng_cpr_array(1,1:nr);
real_cpr=rng_cpr_array(2:2:2*nd,1:nr);
imag_cpr=rng_cpr_array(3:2:1+2*nd,1:nr);
%
% Store the complex pressure in an array (nd x nr) with nd rows
% (number of receiver depths) and nr columns (number of ranges)
% 
cpr=real_cpr+i*imag_cpr;
% 
% Choose receiver depth number to compute transmission and plot 
%
nd_plt=1
rec_depth=rd(nd_plt)
tl=-20*log10(abs(cpr(nd_plt,1:nr)));
% 
plot(rng,tl);
set(gca,'YDir','reverse');