<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Extracting peaks from sine wave data in Discussions</title>
    <link>https://community.jmp.com/t5/Discussions/Extracting-peaks-from-sine-wave-data/m-p/627850#M82626</link>
    <description>&lt;P&gt;Thank you so much for spending the time to write this detailed reply! I got it to work which is fantastic, thank you again!&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Tue, 02 May 2023 05:32:46 GMT</pubDate>
    <dc:creator>Steph136</dc:creator>
    <dc:date>2023-05-02T05:32:46Z</dc:date>
    <item>
      <title>Extracting peaks from sine wave data</title>
      <link>https://community.jmp.com/t5/Discussions/Extracting-peaks-from-sine-wave-data/m-p/626498#M82532</link>
      <description>&lt;P&gt;Hi I have some motion data of cyclic movement measured as angle over time. Is there a way to extract the minimum and maximum peak values (I am a noob so a simple explanation is possible please). An example below. I have been doing it manually but I have some data with a large number of peaks so would like a more efficient way. Many Thanks&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Steph136_0-1682554488533.png" style="width: 400px;"&gt;&lt;img src="https://community.jmp.com/t5/image/serverpage/image-id/52391iD9E943F1C480EC60/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Steph136_0-1682554488533.png" alt="Steph136_0-1682554488533.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 09 Jun 2023 16:08:58 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/Extracting-peaks-from-sine-wave-data/m-p/626498#M82532</guid>
      <dc:creator>Steph136</dc:creator>
      <dc:date>2023-06-09T16:08:58Z</dc:date>
    </item>
    <item>
      <title>Re: Extracting peaks from sine wave data</title>
      <link>https://community.jmp.com/t5/Discussions/Extracting-peaks-from-sine-wave-data/m-p/626568#M82534</link>
      <description>&lt;P&gt;Here is an example that finds the top and bottom peaks for the sine waves.&amp;nbsp; The script first generates some example data and then goes and finds the top and bottom peaks.&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="txnelson_0-1682573652002.png" style="width: 900px;"&gt;&lt;img src="https://community.jmp.com/t5/image/serverpage/image-id/52401i9A15FEFF38DAECCC/image-dimensions/900x354?v=v2" width="900" height="354" role="button" title="txnelson_0-1682573652002.png" alt="txnelson_0-1682573652002.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;Names Default To Here( 1 );
dt = New Table( "sine", New Column( "x" ), New Column( "y" ) );
For( k = 1, k &amp;lt;= 10, k++,
	yfudgepos = Random Uniform( 0, .2 );
	yfudgeneg = Random Uniform( 0, .2 );
	For( x = 0, x &amp;lt;= 360, x++,
		dt &amp;lt;&amp;lt; add rows( 1 );
		y = Sin( x / 180 * Pi() );
		:x[Row()] = x + ((k - 1) * 360);
		If( y &amp;lt; -1 * yfudgeneg,
			:y[Row()] = y + yfudgeneg
		);
		If( y &amp;gt; yfudgepos,
			:y[Row()] = y - yfudgePos
		);
	);
);
dt &amp;lt;&amp;lt; select where(ismissing(:y));
dt &amp;lt;&amp;lt; delete rows;

Graph Builder( Variables( X( :x ), Y( :y ) ), Elements( Points( X, Y, Legend( 3 ) ) ) );

wait(5);


// Find the min and max for each phase in the sine waves
dt &amp;lt;&amp;lt; New Column("T or B",character);

// determine the starting direction of the wave
if(dt:y[1]&amp;lt;dt:y[2], direction = "up", direction = "down");

For( i = 1, i &amp;lt;= N Rows( dt ) - 1, i++,
	If(
		direction == "up" &amp;amp; dt:y[i + 1] &amp;lt; dt:y[i],
		show(i);
			dt:T or B[i] = "Top";
			direction = "down";,
		direction == "down" &amp;amp; dt:y[i + 1] &amp;gt; dt:y[i],
			dt:T or B[i] = "Bottom";
			direction = "up";
	)
);

dt &amp;lt;&amp;lt; select where( :T or B != "" );

dtTB = dt &amp;lt;&amp;lt; subset( selected columns(0), selected rows(1));&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 27 Apr 2023 05:35:20 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/Extracting-peaks-from-sine-wave-data/m-p/626568#M82534</guid>
      <dc:creator>txnelson</dc:creator>
      <dc:date>2023-04-27T05:35:20Z</dc:date>
    </item>
    <item>
      <title>Re: Extracting peaks from sine wave data</title>
      <link>https://community.jmp.com/t5/Discussions/Extracting-peaks-from-sine-wave-data/m-p/627577#M82609</link>
      <description>&lt;P&gt;Thank you so much for this. Sorry for the silly questions, how do I then apply this to my data. Also could you explain why there is a little step along the 0?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Steph&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 30 Apr 2023 23:42:25 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/Extracting-peaks-from-sine-wave-data/m-p/627577#M82609</guid>
      <dc:creator>Steph136</dc:creator>
      <dc:date>2023-04-30T23:42:25Z</dc:date>
    </item>
    <item>
      <title>Re: Extracting peaks from sine wave data</title>
      <link>https://community.jmp.com/t5/Discussions/Extracting-peaks-from-sine-wave-data/m-p/627598#M82611</link>
      <description>&lt;P&gt;I should have been more descriptive in previous response.&lt;/P&gt;
&lt;P&gt;The ability to find the peaks and valleys of a sine wave using JMP is not one of JMP's&amp;nbsp; built in&amp;nbsp; analyses.&amp;nbsp; However, by using JMP's Scripting Language(JSL) a script can be used to get the information you need.&lt;/P&gt;
&lt;P&gt;The first thing I needed to start working on a solution to your question was a sample data table.&amp;nbsp; JMP provides in it's installation however, I am not aware of any of them that provides the data values for a sine wave similar to the sine wave you provided in your initial question.&amp;nbsp; I therefore had to create my own sample data table.&amp;nbsp; The first part of my script does nothing more than create a sample data table.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;Names Default To Here( 1 );
dt = New Table( "sine", New Column( "x" ), New Column( "y" ) );
For( k = 1, k &amp;lt;= 10, k++,
	yfudgepos = Random Uniform( 0, .2 );
	yfudgeneg = Random Uniform( 0, .2 );
	For( x = 0, x &amp;lt;= 360, x++,
		dt &amp;lt;&amp;lt; add rows( 1 );
		y = Sin( x / 180 * Pi() );
		:x[Row()] = x + ((k - 1) * 360);
		If( y &amp;lt; -1 * yfudgeneg,
			:y[Row()] = y + yfudgeneg
		);
		If( y &amp;gt; yfudgepos,
			:y[Row()] = y - yfudgePos
		);
	);
);
dt &amp;lt;&amp;lt; select where(ismissing(:y));
dt &amp;lt;&amp;lt; delete rows;Th&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;This part of the script create the sample data table.&amp;nbsp; It is not a perfect sine wave data table.&lt;/P&gt;
&lt;P&gt;I wanted the produced sine wave to have some variability for each wave. To accomplish this, the code adjusts the wave each time it goes above or below the mid&amp;nbsp; of the wave. .The end result is a wave that has different maximum high points, and different minimum values for each wave.&amp;nbsp; The end resulting data table has that variability, but it also creates a little step in the wave form.&amp;nbsp; While the sample data table isn't perfect, it is good enough to use.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="txnelson_0-1682933354067.png" style="width: 400px;"&gt;&lt;img src="https://community.jmp.com/t5/image/serverpage/image-id/52478i54A11211E59A6DF8/image-size/medium?v=v2&amp;amp;px=400" role="button" title="txnelson_0-1682933354067.png" alt="txnelson_0-1682933354067.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;Here is the part of the script that produces the data for the above wave.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;Names Default To Here( 1 );
dt = New Table( "sine", New Column( "x" ), New Column( "y" ) );
For( k = 1, k &amp;lt;= 10, k++,
	yfudgepos = Random Uniform( 0, .2 );
	yfudgeneg = Random Uniform( 0, .2 );
	For( x = 0, x &amp;lt;= 360, x++,
		dt &amp;lt;&amp;lt; add rows( 1 );
		y = Sin( x / 180 * Pi() );
		:x[Row()] = x + ((k - 1) * 360);
		If( y &amp;lt; -1 * yfudgeneg,
			:y[Row()] = y + yfudgeneg
		);
		If( y &amp;gt; yfudgepos,
			:y[Row()] = y - yfudgePos
		);
	);
);
dt &amp;lt;&amp;lt; select where(ismissing(:y));
dt &amp;lt;&amp;lt; delete rows;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;The table it produces looks like&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="txnelson_1-1682933577226.png" style="width: 400px;"&gt;&lt;img src="https://community.jmp.com/t5/image/serverpage/image-id/52479i8C6AA51A0B1D787C/image-size/medium?v=v2&amp;amp;px=400" role="button" title="txnelson_1-1682933577226.png" alt="txnelson_1-1682933577226.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;The data table I created has two column.&amp;nbsp; One called X and one called Y&amp;nbsp; The columns can be named whatever one would like to call them.&amp;nbsp; The script just has to be changed to swap out the references to the column names of X and Y to whatever the actual names of the columns are.&lt;/P&gt;
&lt;P&gt;Getting back to the actual script that I provided....once the data table is created, I then used Graph Builder to display the data in a graphical form.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;Graph Builder( Variables( X( :x ), Y( :y ) ), Elements( Points( X, Y, Legend( 3 ) ) ) );&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;This JSL statement displayed the sine wave graph.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The next statement simple pauses the execution of the script, to allow one to see the graph for 5 seconds, before moving on to the actual searching for the minimum and maximum values in the data.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;Wait(  5 );&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Theremainder of the JSL, is the actual working part of the code, that first finds the maximum and minimum point of each sine wave, and then creates a new data table containing just the&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Top and Bottom data points for each wave.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;// Find the min and max for each phase in the sine waves
dt &amp;lt;&amp;lt; New Column("T or B",character);

// determine the starting direction of the wave
if(dt:y[1]&amp;lt;dt:y[2], direction = "up", direction = "down");

For( i = 1, i &amp;lt;= N Rows( dt ) - 1, i++,
	If(
		direction == "up" &amp;amp; dt:y[i + 1] &amp;lt; dt:y[i],
		show(i);
			dt:T or B[i] = "Top";
			direction = "down";,
		direction == "down" &amp;amp; dt:y[i + 1] &amp;gt; dt:y[i],
			dt:T or B[i] = "Bottom";
			direction = "up";
	)
);

dt &amp;lt;&amp;lt; select where( :T or B != "" );

dtTB = dt &amp;lt;&amp;lt; subset( selected columns(0), selected rows(1));&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;To apply this analysis to your data, you will first need to input your data into a JMP data table in a structure like the sample data table I generated.&amp;nbsp; Then, assuming your data column were named X and Y, you just need to run the script below, to have it use your new table to analyze the data and find the minimum and maximum values for each wave.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;Names Default To Here( 1 );
dt = Current Data Table();

// Find the min and max for each phase in the sine waves
dt &amp;lt;&amp;lt; New Column( "T or B", character );

// determine the starting direction of the wave
If( dt:y[1] &amp;lt; dt:y[2],
	direction = "up",
	direction = "down"
);

For( i = 1, i &amp;lt;= N Rows( dt ) - 1, i++,
	If(
		direction == "up" &amp;amp; dt:y[i + 1] &amp;lt; dt:y[i],
			Show( i );
			dt:T or B[i] = "Top";
			direction = "down";,
		direction == "down" &amp;amp; dt:y[i + 1] &amp;gt; dt:y[i],
			dt:T or B[i] = "Bottom";
			direction = "up";
	)
);

dt &amp;lt;&amp;lt; select where( :T or B != "" );

dtTB = dt &amp;lt;&amp;lt; subset( selected columns( 0 ), selected rows( 1 ) );&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 01 May 2023 10:05:20 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/Extracting-peaks-from-sine-wave-data/m-p/627598#M82611</guid>
      <dc:creator>txnelson</dc:creator>
      <dc:date>2023-05-01T10:05:20Z</dc:date>
    </item>
    <item>
      <title>Re: Extracting peaks from sine wave data</title>
      <link>https://community.jmp.com/t5/Discussions/Extracting-peaks-from-sine-wave-data/m-p/627850#M82626</link>
      <description>&lt;P&gt;Thank you so much for spending the time to write this detailed reply! I got it to work which is fantastic, thank you again!&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 02 May 2023 05:32:46 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/Extracting-peaks-from-sine-wave-data/m-p/627850#M82626</guid>
      <dc:creator>Steph136</dc:creator>
      <dc:date>2023-05-02T05:32:46Z</dc:date>
    </item>
    <item>
      <title>Re: Extracting peaks from sine wave data</title>
      <link>https://community.jmp.com/t5/Discussions/Extracting-peaks-from-sine-wave-data/m-p/627975#M82632</link>
      <description>&lt;P&gt;Please click on "Accept as Solution" if you found an answer useful. It highlights to others that this solution has helped you. :)&lt;/img&gt;&lt;/P&gt;</description>
      <pubDate>Tue, 02 May 2023 16:20:32 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/Extracting-peaks-from-sine-wave-data/m-p/627975#M82632</guid>
      <dc:creator>StarfruitBob</dc:creator>
      <dc:date>2023-05-02T16:20:32Z</dc:date>
    </item>
  </channel>
</rss>

