필터 지우기
필터 지우기

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

조회 수: 1 (최근 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 CenterFile Exchange에서 Spectral Measurements에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by