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

2 次查看(过去 30 天)
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 个评论
dpb
dpb 2021-4-14
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.

请先登录,再进行评论。

回答(1 个)

Ahmed Redissi
Ahmed Redissi 2021-4-14
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

类别

Help CenterFile Exchange 中查找有关 Fourier Analysis and Filtering 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by