clear
close all

%define the input file names generated by splitFile.m
fileName = 'Aug12-2010-1405.09';
tensFileName = strcat(fileName, 'Tens.csv');
xBowFileName = strcat(fileName, 'xBow.csv');
iLinkFileName = strcat(fileName, 'iLink.csv');

%read the tension file
tensStruct = importdata(tensFileName, '\t');
tensionWS = tensStruct.data(:,1);
tensionWS = tensionWS(1:length(tensionWS)-1);    
%deal with NaNs
tensWSNaNIndex = find(isnan(tensionWS));
tensionWS(tensWSNaNIndex) = tensionWS(tensWSNaNIndex-1);

clear tensStruct ;
%define the PSD arguments
Fs = 10.0;
% windowName = 'Hamming';
% segmentLength = 2^12;
% overlapPercent = 50;
% %calculate the PSD
% tensWSHs = spectrum.welch(windowName, segmentLength, overlapPercent);
% tensionWSPSD = psd(tensWSHs, detrend(tensionWS), 'Fs', Fs);
window = 1024;
overlap = [];  %Defaults to 50%
[tensionWSPSD,freqTensionWSPSD] = pwelch(detrend(tensionWS),window,overlap,[],Fs);

%read the buoy xBow file
xBowStruct = importdata(xBowFileName, '\t');
buoyAccZWS = xBowStruct.data(:,9);
buoyZWS = xBowStruct.data(:,15);

%deal with NaNs
buoyAccZWSNaNIndex = find(isnan(buoyAccZWS));
buoyAccZWS(buoyAccZWSNaNIndex) = buoyAccZWS(buoyAccZWSNaNIndex-1);
buoyZWSNaNIndex = find(isnan(buoyZWS));
buoyZWS(buoyZWSNaNIndex) = buoyZWS(buoyZWSNaNIndex-1);

clear xBowStruct
%define the PSD arguments
Fs = 10.0;
% windowName = 'Hamming';
% segmentLength = 2^12;
% overlapPercent = 50;
% %calculate the spectrums
% xBowHs = spectrum.welch(windowName, segmentLength, overlapPercent);
% buoyAccZWSPSD = psd(xBowHs, detrend(buoyAccZWS), 'Fs', Fs);
% buoyZWSPSD = psd(xBowHs, detrend(buoyZWS), 'Fs', Fs);
window = 1024;
overlap = [];  %Defaults to 50%
[buoyAccZWSPSD,freqBuoyAccZWSPSD] = pwelch(detrend(buoyAccZWS),window,overlap,[],Fs);
[buoyZWSPSD,freqBuoyZWSPSD] = pwelch(detrend(buoyZWS),window,overlap,[],Fs);

%read the iLink file
iLinkStruct = importdata(iLinkFileName, '\t');
ESPAccZWS = iLinkStruct.data(:,3);
%deal with NaNs
iLinkWSNaNIndex = find(isnan(ESPAccZWS));
ESPAccZWS(iLinkWSNaNIndex) = ESPAccZWS(iLinkWSNaNIndex-1);
%Skip bad data point
ESPAccZWS = ESPAccZWS(10:length(ESPAccZWS));

clear iLinkStruct ;
%define the PSD arguments
Fs = 10.0;
% windowName = 'Hamming';
% segmentLength = 2^12;
% overlapPercent = 50;
% %calculate the PSD
% ESPAccZWSHs = spectrum.welch(windowName, segmentLength, overlapPercent);
% ESPAccZWSPSD = psd(ESPAccZWSHs, detrend(ESPAccZWS), 'Fs', Fs);
window = 1024;
overlap = [];  %Defaults to 50%
[ESPAccZWSPSD,freqESPAccZWSPSD] = pwelch(detrend(ESPAccZWS),window,overlap,[],Fs);

% plot(tensionWS)
% title('Tension (lbf w/Skirt)');
% figure
% plot(tensionWSPSD)
% title('Tension PSD (w/Skirt)');
% figure
% plot(buoyAccZWSPSD)
% title('Buoy Z Acceleration PSD (w/Skirt)');
% figure
% plot(buoyZWSPSD)
% title('Buoy Z PSD (w/Skirt)');
% figure
% plot(ESPAccZWSPSD)
% title('ESP Z Acceleration PSD (w/Skirt)');

save 'procDataWS2';
fclose('all');