%program to analyze ADV data

clear all

%time between measurements
delta_t=0.05;

%load data
amp=load('test3.amp');
vel=load('test3.vel');

%plot data:
m=length(vel(:,5));
figure(1)
subplot(2,1,1)
plot(delta_t*[1:m],vel(:,5),'-k',delta_t*[1:m],vel(:,6),'-b',delta_t*[1:m],vel(:,7),'-r');
xlabel('time [sec]')
ylabel('velocity [cm/s], x-black, y-blue, z-red');

subplot(2,1,2)
plot(delta_t*[1:m],amp(:,5),'-k',delta_t*[1:m],amp(:,6),'-b',delta_t*[1:m],amp(:,7),'-r');
xlabel('time [sec]')
ylabel('amplitude, x-black, y-blue, z-red');



%calculate mean and perturbation of velocity. 
avg_velx=mean(vel(:,5));
avg_vely=mean(vel(:,6));
avg_velz=mean(vel(:,7));

pert_velx=vel(:,5)-avg_velx;
pert_vely=vel(:,6)-avg_vely;
pert_velz=vel(:,7)-avg_velz;

%plot w' vs u':
figure(2)
plot(pert_velx,pert_velz,'.');
xlabel('u_{prime} [cm/s]')
ylabel('w_{prime} [cm/s]')

%compute the shear velocity:
u_star=mean(sqrt(pert_velx.*pert_velz+pert_vely.*pert_velz));

%calculate mean and perturbation of acoustic amplitude (combining all, given redundency). 
amp3=(amp(:,5)+amp(:,6)+amp(:,7))/3;
avg_amp=mean(amp3);
pert_amp=amp3-mean(amp3);

%compute the velocity Reynolds stresses:
uw=mean(pert_velx.*pert_velz);
vw=mean(pert_vely.*pert_velz);
uv=mean(pert_velx.*pert_vely);
uu=mean(pert_velx.*pert_velx);
vv=mean(pert_vely.*pert_vely);
ww=mean(pert_velz.*pert_velz);

%compute the concentration Reynolds stresses:
uc=mean(pert_velx.*pert_amp);
vc=mean(pert_vely.*pert_amp);
wc=mean(pert_velz.*pert_amp);

%plot data to estimate decorrelation times:
m=length(pert_velx);

figure(3)
A=xcorr(pert_velx,'coeff');
plot(delta_t*[1:m],A(m:2*m-1));
xlabel('lag time [sec]');
ylabel('correlation coefficient, velocity');

figure(4)
B=xcorr(pert_amp,'coeff');
plot(delta_t*[1:m],B(m:2*m-1));
xlabel('lag time [sec]');
ylabel('correlation coefficient, amplitude');
