Returning coordinates from a plot function

2 vues (au cours des 30 derniers jours)
Trent
Trent le 8 Août 2012
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');

Réponse acceptée

Wayne King
Wayne King le 8 Août 2012
Modifié(e) : Wayne King le 8 Août 2012
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 commentaire
Trent
Trent le 9 Août 2012
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

Connectez-vous pour commenter.

Plus de réponses (1)

Honglei Chen
Honglei Chen le 8 Août 2012
I would also suggest you to take a look at findpeaks function in case you need to detect multiple peaks.

Community Treasure Hunt

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

Start Hunting!

Translated by