Clear Filters
Clear Filters

How can i make a function that return values from a plot?

4 views (last 30 days)
Hello!
I need to write a function that tells me the value of spectral components from a plot (the y-axis correspondant of the frequency on the x-axis). Can i make a function that tells me the value straight from the plot? If so, than how can i do that. Otherwise, can i make a function that calculates the vallue directly?
The plot is a Discret Fourier Transform that i made using fft funtion.
  1 Comment
dpb
dpb on 14 Apr 2021
How do you decide which spectral component you want? You have to have computed the frequency of the x-axis in order to have plotted versus frequency to have started with; do you want the nearest frequency bin, the integral of the peak associated with a frequency (the frequency input of a given signal may/probably won't match identically the centerline of a frequency bin in the FFT), or ... ???
Far too nebulous of a Q? to be able to answer.

Sign in to comment.

Answers (1)

Ahmed Redissi
Ahmed Redissi on 14 Apr 2021
If you only have a figure and not the data, you can follow this example to extract data from the figure containing the FFT:
clear
clc
close all
% Generate a signal
Fs = 1000; % Sampling frequency (Hz)
t = 0:1/Fs:0.3; % Time vector (s)
Nt = length(t);
f = (-Nt/2:Nt/2-1)*(Fs/Nt); % Frequency vector (Hz)
x = cos(2*pi*t*200); % Signal
% Calculate the FFT
y = fftshift(fft(x));
% Convert the magnitude to dB
ydb = 20*log10(abs(y));
% Plot the FFT magnitude
plot(f,ydb)
grid
xlabel('Frequency (Hz)')
ylabel('FFT Magnitude (dB)')
% Get the figure handle
h = findall(0,'Type','Figure');
% Get the FFT value from the figure
findex = 212; % Frequency index for 200 Hz
yvalue = getYValueFromFigure(h,findex);
function yvalue = getYValueFromFigure(h,index)
data = findall(h,'Type','Line');
ydata = data.YData;
yvalue = ydata(index);
end

Categories

Find more on Fourier Analysis and Filtering 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!