How to add a vertical line in a plot that corresponds to a maximum?
14 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Petros Tsitouras
am 5 Jul. 2019
Kommentiert: Star Strider
am 5 Jul. 2019
Hello, everyone!
I would like to add a vertical line corresponding to the maximum value of the graph attached (and displaying its value). Basically, I need a line that is 'x= fundamental frequency' . I know that similar questions are already answered in the forum, but could not find something working for me. Below I have attached the code and the audio file, accompanied with the last graph that I want to add the line to. Thank you very much in advance!
close all;
clear all;
clc;
%______________________________________________________
[y, Fs] = audioread('A1-sound.wav');
t=linspace(0,length(y)/Fs, length(y));
Fn = Fs/2;
L = length(y);
Y = fft(y);
phase2 = abs(Y/L);
phase1 = phase2(1:L/2+1);
phase1(2:end-1) = 2*phase1(2:end-1);
F = Fs*(0:(L/2))/L;
%_______________________________Frequencies Distribution
figure;
subplot(2,1,1);
plot(F,phase1);
title('Amplitude Spectrum of Signal');
xlabel('Frequency (Hz)');
ylabel('Absolute Amplitude of Frequencies (dB)');
%__________________________________________________Focus
idx = F >= 0 & F <= 1000; %Focusing 0-1000 Hz
zoomX = F(idx);
zoomY = phase1(idx);
subplot(2,1,2);
plot(zoomX, zoomY);
title('Amplitude Spectrum of Signal');
xlabel('Frequency (Hz)');
ylabel('Absolute Amplitude of Frequencies (dB)');
%________________________________________Fundamental Frequency
[maxValue, f1] = max(Y);
idx2 = F >= 80 & F <= 120; %Focusing 80-120 Hz
zoomX2 = F(idx2);
zoomY2 = phase1(idx2);
figure;
plot(zoomX2, zoomY2);
title('Amplitude Spectrum of Signal');
xlabel('Frequency (Hz)');
ylabel('Absolute Amplitude of Frequencies (dB)');
0 Kommentare
Akzeptierte Antwort
Star Strider
am 5 Jul. 2019
Changing part of your code to draw the vertical lines, and the text label in figure(2):
%__________________________________________________Focus
idx = F >= 0 & F <= 1000; %Focusing 0-1000 Hz
zoomX = F(idx);
zoomY = phase1(idx);
subplot(2,1,2);
plot(zoomX, zoomY);
title('Amplitude Spectrum of Signal');
xlabel('Frequency (Hz)');
ylabel('Absolute Amplitude of Frequencies (dB)');
[pks1,locs1] = findpeaks(zoomY, 'MinPeakProminence',0.002, 'MinPeakDistance',100); % Identify Prominent Peaks ...
hold on
ylv = ylim;
plot([1;1]*zoomX(locs1), ylv(:)*ones(size(pks1'))) % ... & Plot Vertical Lines Through Them
hold off
%________________________________________Fundamental Frequency
[maxValue, f1] = max(Y);
idx2 = F >= 80 & F <= 120; %Focusing 80-120 Hz
zoomX2 = F(idx2);
zoomY2 = phase1(idx2);
figure;
plot(zoomX2, zoomY2);
title('Amplitude Spectrum of Signal');
xlabel('Frequency (Hz)');
ylabel('Absolute Amplitude of Frequencies (dB)');
[pks2,locs2] = max(zoomY2); % Identify Prominent Peak ...
hold on
plot([1;1]*zoomX2(locs2)', ylv(:)*ones(size(pks2'))) % ... & Plot Vertical Line Through It
text(zoomX2(locs2), pks2/2, sprintf('\\leftarrow %.1f Hz = Fundamental Frequency',zoomX2(locs2)), 'HorizontalAlignment','left')
I wasn’t certain if you also wanted the vertical lines in the lower subplot, so I used findpeaks to add them as well. The peak in figure(2) kist needs max, since ther is only one peak.
Experiment to get the result you want.
4 Kommentare
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Waveform Generation finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!