Returning coordinates from a plot function
조회 수: 2 (최근 30일)
이전 댓글 표시
Hello fellow members,
I want to return the x coordinate as a stored value (in my case the frequency in Hz) at the point that the plot of F,db (which is the difference between the plots of Px_f and Px_g as a function of F) has the maximum value (or in other words, the maximum difference). Obviously I can get the max by max(db) which returns the max value, however I need the frequency (along the x-axis) at that point.
data1= 65536x1 double array data2= 65536x1 double array
Just for some background. Data1 is faulty bearing data and data2 is good bearing data recorded from a data logger.
Code is below (If I haven't explained myself correctly then please let me know).
Thanks for your help.
% code
fs=48000; % Sampling rate at 48000Hz
figure
[Px_f,F]=pwelch(data1,1000,500,[],fs);
plot(F,20*log10(Px_f),'r');
hold on
[Px_g,F]=pwelch(data2,1000,500,[],fs);
plot(F,20*log10(Px_g),'g');
hold on
db=20*log10(Px_f)-20*log10(Px_g);
plot(F,db,'k');
grid on
xlabel('Frequency (Hz)')
ylabel('Power/frequency (dB/Hz)');
title('Welch Power Spectrum Density')
legend('Faulty Bearing','Good Bearings','Variance','Location','best');
댓글 수: 0
채택된 답변
Wayne King
2012년 8월 8일
편집: Wayne King
2012년 8월 8일
Hi Trent, max() will return the index also and from that index, you can get the maximum frequency from the frequency vector that pwelch returns by querying the value of the frequency vector at the index returned by max(). Keep in mind however, that pwelch does not have the best frequency resolution among nonparametric spectral estimators. I'll give you an example with periodogram(), but the workflow is the same if you want to use pwelch
Fs = 1000;
t = 0:1/Fs:1-1/Fs;
% 100-Hz sine wave in noise
x = cos(2*pi*100*t)+randn(size(t));
% I'll just use periododgram, but it's the same
[Pxx,F] = periodogram(x,rectwin(length(x)),length(x),Fs);
[val,I] = max(Pxx);
F(I)
추가 답변 (1개)
Honglei Chen
2012년 8월 8일
I would also suggest you to take a look at findpeaks function in case you need to detect multiple peaks.
댓글 수: 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!