- Pxx is initialized as a 3D matrix and f as a 2D matrix with the proper dimensions.
- The pwelch function is called for each slice of u_prime corresponding to each iteration of the loop.
- The output Pxx_slice from pwelch is assigned to the corresponding slice of the Pxx matrix.
- The frequency vector f_slice only needs to be assigned once since it remains constant for each call to pwelch, so it is assigned outside of the loop.
Estimate the frequency and power spectra for a signal using Pwelch command
조회 수: 7 (최근 30일)
이전 댓글 표시
Hello,
I am new in Mathlab, I am trying to estimate the frequency and power spectra for a signal using Pwelch command using the below command:
u_prime=u1-u_m1
for pp=1:size(u1)
[Pxx(pp,:),f(pp,:)] = pwelch(u_prime(pp,:),Fs,[],[],Fs,'onesided');
end
the size of u1 and u_prime is 512x512x1500 and I want to get the Pxx and f as also 512x512x129. my problem is with the used command I get a matrix of 512x129.
can somebody help me in correcting the Mathlab command?
Thanks in advance
Mohamed
댓글 수: 0
답변 (1개)
Hassaan
2023년 12월 21일
The issue seems to be with how you are indexing into Pxx and f inside the loop. You are using the loop variable pp to index into the third dimension of u_prime and expect Pxx to accumulate along the third dimension for each iteration. However, the pwelch function only returns a 2D matrix for the power spectral density estimate, which explains why you are getting 512x129.
To obtain a 512x512x129 matrix, the user needs to initialize Pxx and f with the correct dimensions outside the loop and then assign the output of pwelch to the correct slice of Pxx on each iteration.
% Assuming 'u1' and 'u_prime' are defined and Fs is the sampling frequency
% Initialize Pxx and f with the correct dimensions
Pxx = zeros(512, 512, 129);
f = zeros(512, 129);
for pp = 1:size(u1, 1)
[Pxx_slice, f_slice] = pwelch(u_prime(pp,:,:), [], [], [], Fs, 'onesided');
% Assign the output to the correct slice of Pxx and f
Pxx(pp, :, :) = Pxx_slice;
% Only need to assign f once since it should be the same for all slices
if pp == 1
f(pp, :) = f_slice;
end
end
% f should only have two dimensions, so remove the singleton dimension
f = squeeze(f(1, :));
In this corrected code:
Note that the pwelch function returns a vector f_slice and a matrix Pxx_slice for the power spectral density. The size of Pxx_slice will depend on the window and overlap specified, as well as the nfft parameter, which is not explicitly defined in the code above. If the user wants to control the frequency resolution, they will need to specify the nfft parameter in the pwelch call.
Also, I'm assuming u_prime is a 3D matrix where the second dimension (time or samples) is the one along which pwelch should calculate the power spectral density. If that's not the case, the user will need to adjust the dimensions accordingly.
If you liked the anwer and it solved your problem. Please do leave a upvote and a comment means a lot. Thank you.
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Spectral Estimation에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!