How can I disregard part of the signal when calculating midline of my data signal?

1 次查看(过去 30 天)
I have a waveforem that starts regular oscillations after like 2 seconds of running the program. How can I get midline of the remaining signal after T=2, so basically disreagrading the first 2 seconds of the output wave. Also, is there an easy way to find frequency of this signal(Again while disregarding the first 2 seconds of wave)?I have attached a sample wave for reference.
Currently I am just using:
lnsig=length(y);
midline = mean(y)*ones(1,lnsig)
Also, if there's a way that I can keep changing the time after which I calculate for midline that would be great!

采纳的回答

Mathieu NOE
Mathieu NOE 2022-11-14
hello
see code below for midline and signal oscillation frequency computation (cycle to cycle "zero" crossing detection)
a PID tuning problem ?
data = extract_data_from_figures('2.fig');
VN = data.names;
data = data.Y;
% {'X' }
% {'midline'}
% {'target' }
% {'y vs x' }
x = data(:,1);
y_target = data(:,3);
y = data(:,4);
idx = (x>=1); % valid data for t > 1 s
midline = mean(y(idx));
%%find frequency of this signal
threshold = midline; %
t0_pos1 = find_zc(x,y,threshold);
period = diff(t0_pos1); % delta time of crossing points
freq = 1./period; % signal frequency
figure(1)
subplot(2,1,1),plot(x,y,'b',x,y_target,'g',x(idx),midline*ones(size(x(idx))),'k--',t0_pos1,midline*ones(size(t0_pos1)),'dr','Linewidth',2);
legend('signal','target','midline','crossing points');
xlabel('Time(s)')
subplot(2,1,2),plot(t0_pos1(2:end),freq,'b.-','linewidth',2,'markersize',12);grid on
xlim([min(x) max(x)]);
legend('signal rate (frequency)');
xlabel('Time(s)')
ylabel('Freq(Hz)')
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function [Zx] = find_zc(x,y,threshold)
% positive slope "zero" crossing detection, using linear interpolation
y = y - threshold;
zci = @(data) find(diff(sign(data))>0); %define function: returns indices of +ZCs
ix=zci(y); %find indices of + zero crossings of x
ZeroX = @(x0,y0,x1,y1) x0 - (y0.*(x0 - x1))./(y0 - y1); % Interpolated x value for Zero-Crossing
Zx = ZeroX(x(ix),y(ix),x(ix+1),y(ix+1));
end
function data = extract_data_from_figures(filename)
%%
%
% Input : File name <filename.fig>
% with multiple plots in a single file
% Output : struct data
% : data.names contains names of the display object Yvalues
% : data.Y contains the actual plot values withthe first column
% containing the x-values
%
% Written by Chetanya Puri, 2019
% Last Modified: Nov 6, 2019
%
fig = openfig(filename); % Open figure and assign it to fig object
dataObjs = findobj(fig,'-property','YData'); % Find all graphic objects with YData, in our case line values
xval = dataObjs(1).XData; % Find the X-axis value
Ymat = [xval(:)]; % Create a matrix with first column of x values
for i=1:length(dataObjs)
legend_name{i,1} = dataObjs(i).DisplayName;
yval = dataObjs(i).YData;
Ymat = [Ymat yval(:)]; % Keep appending column vectors
end
close(fig); % close the figure
data.names = ['X';legend_name];
data.Y = Ymat;
end
  4 个评论

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Sources 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by