clear
close all

%define the input file names generated by splitFile.m
fileName = 'Aug12-2010-1442.56';
tensFileName = strcat(fileName, 'Tens.csv');
xBowFileName = strcat(fileName, 'xBow.csv');
iLinkFileName = strcat(fileName, 'iLink.csv');

%read the tension file
tensStruct = importdata(tensFileName, '\t');
tension = tensStruct.data(:,1);
tension(1:length(tension)-1);    
tensionNS = tension(30000:length(tension)-1);
%deal with NaNs
tensNSNaNIndex = find(isnan(tensionNS));
tensionNS(tensNSNaNIndex) = tensionNS(tensNSNaNIndex-1);

clear tensStruct ;
%define the PSD arguments
Fs = 10.0;
% windowName = 'Hamming';
% segmentLength = 2^12;
% overlapPercent = 50;
% %calculate the PSD
% tensNSHs = spectrum.welch(windowName, segmentLength, overlapPercent);
% tensionNSPSD = psd(tensNSHs, detrend(tensionNS), 'Fs', Fs);
window = 1024;
overlap = [];  %Defaults to 50%
[tensionNSPSD,freqTensionNSPSD] = pwelch(detrend(tensionNS),window,overlap,[],Fs);

%read the buoy xBow file
xBowStruct = importdata(xBowFileName, '\t');
buoyAccZ = xBowStruct.data(:,9);
buoyZ = xBowStruct.data(:,15);
buoyAccZNS = buoyAccZ(30000:length(buoyAccZ)-40);
buoyZNS = buoyZ(30000:length(buoyAccZ)-40);

%deal with NaNs
buoyAccZNSNaNIndex = find(isnan(buoyAccZNS));
buoyAccZNS(buoyAccZNSNaNIndex) = buoyAccZNS(buoyAccZNSNaNIndex-1);
buoyZNSNaNIndex = find(isnan(buoyZNS));
buoyZNS(buoyZNSNaNIndex) = buoyZNS(buoyZNSNaNIndex-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);
% buoyAccZNSPSD = psd(xBowHs, detrend(buoyAccZNS), 'Fs', Fs);
% buoyZNSPSD = psd(xBowHs, detrend(buoyZNS), 'Fs', Fs);
window = 1024;
overlap = [];  %Defaults to 50%
[buoyAccZNSPSD,freqBuoyAccZNSPSD] = pwelch(detrend(buoyAccZNS),window,overlap,[],Fs);
[buoyZNSPSD,freqBuoyZNSPSD] = pwelch(detrend(buoyZNS),window,overlap,[],Fs);

%read the iLink file
iLinkStruct = importdata(iLinkFileName, '\t');
ESPAccZ = iLinkStruct.data(:,3);
ESPAccZNS = ESPAccZ(30000:length(ESPAccZ));

%deal with NaNs
iLinkNSNaNIndex = find(isnan(ESPAccZNS));
ESPAccZNS(iLinkNSNaNIndex) = ESPAccZ(iLinkNSNaNIndex-1);

clear iLinkStruct ;
%define the PSD arguments
Fs = 10.0;
% windowName = 'Hamming';
% segmentLength = 2^12;
% overlapPercent = 50;
% %calculate the PSD
% ESPAccZNSHs = spectrum.welch(windowName, segmentLength, overlapPercent);
% ESPAccZNSPSD = psd(ESPAccZNSHs, detrend(ESPAccZNS), 'Fs', Fs);
window = 1024;
overlap = [];  %Defaults to 50%
[ESPAccZNSPSD,freqESPAccZNSPSD] = pwelch(detrend(ESPAccZNS),window,overlap,[],Fs);

% plot(tensionNS)
% title('Tension (lbf w/Skirt)');
% figure
% plot(tensionNSPSD)
% title('Tension PSD (w/Skirt)');
% figure
% plot(buoyAccZNSPSD)
% title('Buoy Z Acceleration PSD (w/Skirt)');
% figure
% plot(buoyZNSPSD)
% title('Buoy Z PSD (w/Skirt)');
% figure
% plot(ESPAccZNSPSD)
% title('ESP Z Acceleration PSD (w/Skirt)');

save 'procDataNS2';
fclose('all');