Returning coordinates from a plot function
2 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
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 commentaires
Réponse acceptée
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)
Plus de réponses (1)
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.
0 commentaires
Voir également
Catégories
En savoir plus sur Spectral Estimation dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!