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

2 ビュー (過去 30 日間)
Georgiana Andreianu
Georgiana Andreianu 2021 年 4 月 14 日
回答済み: Ahmed Redissi 2021 年 4 月 14 日
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 Center および File ExchangeFourier Analysis and Filtering についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by