Returning coordinates from a plot function

조회 수: 2 (최근 30일)
Trent
Trent 2012년 8월 8일
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');

채택된 답변

Wayne King
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
Trent
Trent 2012년 8월 9일
Thanks very much Wayne, I have got it to work. Is it possible to find the highest variance value for a F (frequency) value greater then a certain value? Due to environmental factors my data has a large amount of variance in the low frequency range and therefore in certain cases Matlab determines that the difference is the highest in that low frequency range. Whilst this is technically correct its not what I want. Can I make it determine the highest value at say a frequency range greater then 5000Hz for example? Thanks very much. Trent

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Honglei Chen
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.

카테고리

Help CenterFile Exchange에서 Spectral Estimation에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by