plotting points on my sine curve

조회 수: 2 (최근 30일)
Osita Onyejekwe
Osita Onyejekwe 2016년 11월 7일
편집: Osita Onyejekwe 2016년 11월 7일
Hello, I need help. Below is my code for a sine curve with plots across showing the change points. I want to add a different shape for the positive change points (going from up to down on the zero line) and the negative change points (going from down to up on the zero-line) using the code that I have that calculates the indices using the second derivative and the gradient function. The plots across are in red, so i want it to be like red-blue-red-blue-red-blue.etc..or a different shape,. I just want those indices to be distinguished. I don't want to use diff or anything else, I just want to modify my already existing code that utilizes gradient for the second derivative calculation. Here it is below. Thanks alot people! ..I believe the line to modify in the code is this :
indices_corrupted = [find(secondDrvY_corrupted(2:end).*secondDrvY_corrupted(1:end-1)<0) find(abs(secondDrvY_corrupted)<1e-15)]; % you are dealing with a discrete function here so the second derivative may not be exactly at zero x_indices_corrupted = t(indices_corrupted);
But below is the fully working code thus far including the lines from above. Please note that basically whenever the first derivative is greater than zero, we have a point and when the first derivative is less than zero we have a point, so i want to incorporate that first derivative being greater than zero to have a different sign or color for the change point. The first derivative is also super-imposed on the plot. ********************************************************************************* clear close all clc
x = 1:500; X = x; X_transp = x'; J = 1; Fs = 499; N = J*Fs; t = 0: 1/Fs : J; Fn = 20; % this control the number of cycles/periods deltaJ = 0.0020; deltax = 1;
y_sine_25HZ = sin(Fn*2*pi*t); y = y_sine_25HZ ; Y_transp = y'
%************************************************************************** sigma = 0.001; y_corrupted = Y_transp + sigma*randn(length(X_transp),1); %**************************************************************************
%************************************************************************** plot(x,y, 'k.') drvY = gradient(y); % First Derivative Using Gradient secondDrvY = gradient(drvY); % Second Derivative Using Gradient %**************************************************************************
%************************************************************************** drvY_corrupted = gradient(y_corrupted); % First Derivative of the Corrupted Signal secondDrvY_corrupted = gradient(drvY_corrupted); % Second Derivative of the Corrupted Signal %**************************************************************************
%************************************************************************** indices = [find(secondDrvY(2:end).*secondDrvY(1:end-1)<0) find(abs(secondDrvY)<1e-15)]; % you are dealing with a discrete function here so the second derivative may not be exactly at zero x_indices = t(indices); %**************************************************************************
%************************************************************************** indices_corrupted = [find(secondDrvY_corrupted(2:end).*secondDrvY_corrupted(1:end-1)<0) find(abs(secondDrvY_corrupted)<1e-15)]; % you are dealing with a discrete function here so the second derivative may not be exactly at zero** x_indices_corrupted = t(indices_corrupted); %**************************************************************************
dy=[0 sign(diff(y))]; % First derivative locdn = find((diff(dy))== 2); % location down (second derivative) locup = find((diff(dy))==-2); % location up (second derivatie) plot(t,y) ylim([-1.05 1.05]) hold on % Peaks and Troughs scatter(t(locup)',y(locup)',30,'r','*') scatter(t(locdn)',y(locdn)',30,'g','d','filled') plot(t(indices_corrupted), zeros(1,length(indices_corrupted)), 'd', 'MarkerSize', 10, 'MarkerFaceColor', 'r') % change points after added noise plot(t, secondDrvY_corrupted, 'o') plot(t, drvY_corrupted, '--') %plot(t,y_corrupted, 'o') legend('Signal', 'Approximate Zero-Crossings') hold off grid

답변 (0개)

카테고리

Help CenterFile Exchange에서 Pulse and Transition Metrics에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by