MATLAB:提取与最大 y 值匹配的 x 坐标
MATLAB: Extracting x coordinate that matches up with largest y value
使用matlab从csv文件中提取值,对部分数据进行fft变换,然后绘制。在看图时,我看到一个清晰的峰(见附图),我想写一些东西来提取峰的 x 坐标。
这是我写的部分代码。
filename=input('Enter name of a csv file, extension included : ', 's');
csv_file=load(filename);
%file_rows
rows = size(csv_file, 1);
for i = 1:rows-256 %frames
rh_x= csv_file(i:i+255,60); % coordinates of joint 1
rh_y= csv_file(i:i+255,61);
rh_x2 = csv_file(i:i+255, 12); % coordinates of joint 2
rh_y2 = csv_file(i:i+255, 13);
coord = [rh_x, rh_y; rh_x2, rh_y2];
distarray = sqrt( (rh_x + rh_x2).^2 + (rh_y + rh_y2).^2 ); %distance formula
meandist = mean(distarray);
new_arr = distarray - meandist; %subtract mean
transf_new = fft(new_arr); %FFT transform
magn_new = abs(transf_new); %magnitude_output
plot(magn_new(1:size(magn_new,1)/2), '-'); %****I INCLUDED EVERYTHING BUT THIS IS THE PART THAT PLOTS WHAT I WANT TO EXTRACT FROM
if i==1
hold on
end
end
代码为我提供了我需要的所有正确值,我现在只想提取此图中非常明显的峰的 x 值(此图是使用我的一个 CSV 文件生成的)。 我怎样才能写出 store/display 我生成的那个峰的 x 坐标?
谢谢。
使用max
的双输出语法:
[Y,I] = max(X)
returns the indices of the maximum values in vector I.
If the values along the first non-singleton dimension contain more
than one maximal element, the index of the first one is returned.
所以对于给定的数组 magn_new(1:size(magn_new,1)/2)
,
[maxval, imax] = max(magn_new(1:size(magn_new,1)/2));
maxval
将包含最大 y
值,而 imax
将包含相应的 index (在您的情况下恰好是 x
坐标,因为您隐式地绘制了 1:size(magn_new,1)/2
.
由于您是逐批生成数据,因此您可以为每个批存储每个 imax
和 maxval
,或者每次检查新的 maxval
是否更大比之前最大的 maxval
,如果是,则覆盖 imax
.
使用matlab从csv文件中提取值,对部分数据进行fft变换,然后绘制。在看图时,我看到一个清晰的峰(见附图),我想写一些东西来提取峰的 x 坐标。
这是我写的部分代码。
filename=input('Enter name of a csv file, extension included : ', 's');
csv_file=load(filename);
%file_rows
rows = size(csv_file, 1);
for i = 1:rows-256 %frames
rh_x= csv_file(i:i+255,60); % coordinates of joint 1
rh_y= csv_file(i:i+255,61);
rh_x2 = csv_file(i:i+255, 12); % coordinates of joint 2
rh_y2 = csv_file(i:i+255, 13);
coord = [rh_x, rh_y; rh_x2, rh_y2];
distarray = sqrt( (rh_x + rh_x2).^2 + (rh_y + rh_y2).^2 ); %distance formula
meandist = mean(distarray);
new_arr = distarray - meandist; %subtract mean
transf_new = fft(new_arr); %FFT transform
magn_new = abs(transf_new); %magnitude_output
plot(magn_new(1:size(magn_new,1)/2), '-'); %****I INCLUDED EVERYTHING BUT THIS IS THE PART THAT PLOTS WHAT I WANT TO EXTRACT FROM
if i==1
hold on
end
end
代码为我提供了我需要的所有正确值,我现在只想提取此图中非常明显的峰的 x 值(此图是使用我的一个 CSV 文件生成的)。 我怎样才能写出 store/display 我生成的那个峰的 x 坐标?
谢谢。
使用max
的双输出语法:
[Y,I] = max(X)
returns the indices of the maximum values in vector I. If the values along the first non-singleton dimension contain more than one maximal element, the index of the first one is returned.
所以对于给定的数组 magn_new(1:size(magn_new,1)/2)
,
[maxval, imax] = max(magn_new(1:size(magn_new,1)/2));
maxval
将包含最大 y
值,而 imax
将包含相应的 index (在您的情况下恰好是 x
坐标,因为您隐式地绘制了 1:size(magn_new,1)/2
.
由于您是逐批生成数据,因此您可以为每个批存储每个 imax
和 maxval
,或者每次检查新的 maxval
是否更大比之前最大的 maxval
,如果是,则覆盖 imax
.