As per my understanding, you're attempting to calculate the Power Spectral Density (PSD) of a signal using both the “pwelch” function and the “dsp.SpectrumEstimator” object method. However, it seems that the output signals from these two approaches are not identical, even though both utilize the same method to determine the PSD.
The explanation for this is as follows:
- In “dsp.SpectrumEstimator” object, we need to manually segment the signal and step this segment through the object.
- “pwelch” method automatically divides the signal into segments, equal in length. The length of each segment is specified using the “window” property of “pwelch” function.
But, there is an additional input argument for “pwelch” function called “noverlap”. Refer to the below documentation:
The “pwelch” function uses the value of “noverlap” and decides the number of samples to be overlapped between the segments. The script provided in the MATLAB answer link attached by you, specifies “noverlap” as “empty array”. If you do not specify “noverlap”, or specify “noverlap” as empty, the default number of overlapped samples is 50% of the window length. But in the case of “dsp.SpectrumEstimator” object, no samples are overlapped.
Therefore, in the present script, 50% of the samples from segments are getting overlapped in case of “pwelch” function, but no samples are overlapped in case of “dsp.SpectrumEstimator” object.
Due to above stated reason, you are not getting the same output for both the methods.
To resolve the issue, I specified “noverlap” input argument for “pwelch” function as “0”. I am attaching the code below to demonstrate this. Here, I am taking input signal “X” as suggested by you in the question.
fWindow = hann(nfft, 'periodic');
spectrumTypeDsp = 'Power';
[PxxTrue,fTrue] = pwelch(x,fWindow, 0,nfft,fs,freqRange,spectrumType);
seObject2 = dsp.SpectrumEstimator('SampleRate',fs,...
'SpectrumType',spectrumTypeDsp,'Window', 'hann',...
'FFTLengthSource','Property','FFTLength',nfft,...
'SpectralAverages',nrec,'FrequencyRange',freqRange,Method='Welch');
PxxEst = seObject2( x((idx-1)*nfft+1 : idx*nfft) ) ;
fEst = getFrequencyVector(seObject2);
plot(fTrue,db(PxxTrue),fEst,db(PxxEst))
legend('pwelch','dsp.SpectrumEstimator')
title('Correct Welch Power Spectrums ')
Conclusion:
You can see that both the outputs “match perfectly” as desired by you.
I hope this answers your question!