load 'tensionData'

Fs = 10.0;
windowName = 'Hamming';
segmentLength = 1024;
overlapPercent = 50; %Ganesh Comment: This meant 50% Overlap.But below in
                      %PWELCH function you are using 50 point overlap.
%overlapPercent = 100 * 50/1024; %Ganesh Comment: So here I calculate the  
                                % percentage value for 50 point overlap. 

                                
                        
%calculate the PSD
% Ganesh Comment:Spectrum Objects are what we would recommend customers to 
%use.It is much more versatile.
tensWSHsTest = spectrum.welch(windowName, segmentLength, overlapPercent);
tensionWSPSDDT = psd(tensWSHsTest, detrend(tensionWS), 'Fs', Fs);
tensionWSPSDNDT = psd(tensWSHsTest, tensionWS, 'Fs', Fs);

window = 1024;
nFFT = 1024;
overlap = [];
[tensionWSPWelchDT,freqTensionWSPWelchDT] = pwelch(detrend(tensionWS),window,overlap,nFFT,Fs);
[tensionWSPWelchNDT,freqTensionWSPWelchNDT] = pwelch(tensionWS,window,overlap,nFFT,Fs);


% Ganesh Comment: Now the result using Spectrum object & PWELCH match.
figure(1)
plot(tensionWSPSDDT.Frequencies, tensionWSPSDDT.Data)
title 'PSD Detrend'
figure(2)
plot(tensionWSPSDNDT.Frequencies, tensionWSPSDNDT.Data)
title 'PSD No Detrend'
figure(3)
plot(freqTensionWSPWelchDT, tensionWSPWelchDT)
title 'PWelch Detrend'
figure(4)
plot(freqTensionWSPWelchNDT, tensionWSPWelchNDT)
title 'PWelch No Detrend'

