clear sortedSpec spect4OneMonth MSV4OneMonth
close all

monthNum = 9;
index = 1;
spect4OneMonth = [];
for i=1:length(filename)
    a = datestr(WSStruct(i).timeStamp,5);
    b = str2num(a);
    mRows = find(b == monthNum);
    if length(mRows) > 0
        waterfall(WSStruct(i).freqVector,WSStruct(i).timeStamp(mRows),WSStruct(i).spectrum(mRows,:))
    end
    for j=1:length(mRows)
        if (sum(isnan(WSStruct(i).spectrum(mRows(j),:)) == 0))
            spect4OneMonth(index,:) = interp1(WSStruct(i).freqVector, WSStruct(i).spectrum(mRows(j),:),x2,'linear','extrap');
            MSV4OneMonth(index) = WSStruct(i).specMeanValue(mRows(j));
        end
        index = index + 1;
    end
end
datetick('y',2)
titleStr = sprintf('Month %d Spectrums',monthNum);
title(titleStr)
xlabel('Freq (Hz)')
ylabel('Date')
zlabel('Power (m^2/Hz)')

[sortedMSV, index] = sort(MSV4OneMonth);
numIncr = 100.0;
incr = ceil(length(sortedMSV) / numIncr);
j = 1;
for i=1:incr:length(MSV4OneMonth)
    fprintf(1,'%d %d\n',i, i+incr-1);
    temp = spect4OneMonth(index(i:min(length(MSV4OneMonth),i+incr-1)),:);
    sortedSpec(j,:) = mean(temp);
    j = j + 1;
end
figure
plot(x2,sortedSpec',x2,PM_Sw,'-.')
titleStr = sprintf('Mean of Month %d Spectrums sorted into %5.0f bins',monthNum, numIncr);
title(titleStr)
xlabel('Frequency (Hz)')
ylabel('PSD (m^2/Hz)')

figure
plot(x2,20.*log10(sortedSpec'),x2,20*log10(PM_Sw),'-.')
titleStr = sprintf('Mean of Month %d Spectrums sorted into %5.0f bins',monthNum, numIncr);
title(titleStr)
xlabel('Frequency (Hz)')
ylabel('PSD dB(m^2/Hz)')

