How to add a vertical line in a plot that corresponds to a maximum?
Show older comments
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)');
Accepted Answer
More Answers (0)
Categories
Find more on Spectral Measurements in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!