<?xml version="1.0" encoding="UTF-8"?>
<!-- generator="bbPress/1.0.2" -->
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom">
	<channel>
		<title>LeafLabs Garden &#187; Topic: Help with float/fixed math and shortening interrupt time</title>
		<link>http://forums.leaflabs.com/topic.php?id=11334</link>
		<description>A place to share, learn, and grow...</description>
		<language>en-US</language>
		<pubDate>Fri, 22 Jan 2016 00:22:31 +0000</pubDate>
		<generator>http://bbpress.org/?v=1.0.2</generator>
		<textInput>
			<title><![CDATA[Search]]></title>
			<description><![CDATA[Search all topics from these forums.]]></description>
			<name>q</name>
			<link>http://forums.leaflabs.com/search.php</link>
		</textInput>
		<atom:link href="http://forums.leaflabs.com/rss.php?topic=11334" rel="self" type="application/rss+xml" />

		<item>
			<title>mlundinse on "Help with float/fixed math and shortening interrupt time"</title>
			<link>http://forums.leaflabs.com/topic.php?id=11334#post-25615</link>
			<pubDate>Thu, 02 May 2013 10:05:03 +0000</pubDate>
			<dc:creator>mlundinse</dc:creator>
			<guid isPermaLink="false">25615@http://forums.leaflabs.com/</guid>
			<description>&#60;p&#62;I think we already covered this in the Digital Filtering thread.&#60;/p&#62;
&#60;p&#62;Just a few points:&#60;br /&#62;
Make sure that (1-r*c)^2 + c^2 is les than 1, as floating point values&#60;br /&#62;
before scaling them and converting to integers. Otherwise the filter&#60;br /&#62;
becomes unstable and the output pure noise.&#60;/p&#62;
&#60;p&#62;Use signed ints for filter values and filter coefficients then the shifts&#60;br /&#62;
&#38;gt;&#38;gt;16 will use arithmetic and not logical shifts and pad with 0's or 1's&#60;br /&#62;
correctly.&#60;/p&#62;
&#60;p&#62;If you feed the filter with signed integers, scale filter constants by 2^16&#60;br /&#62;
and shift the results by 16 bits to the right then all filter values and&#60;br /&#62;
outputs stays in the same range as the input.&#60;/p&#62;
&#60;p&#62;Regards&#60;br /&#62;
Magnus
&#60;/p&#62;</description>
		</item>
		<item>
			<title>pyrohaz on "Help with float/fixed math and shortening interrupt time"</title>
			<link>http://forums.leaflabs.com/topic.php?id=11334#post-25600</link>
			<pubDate>Wed, 01 May 2013 19:22:23 +0000</pubDate>
			<dc:creator>pyrohaz</dc:creator>
			<guid isPermaLink="false">25600@http://forums.leaflabs.com/</guid>
			<description>&#60;p&#62;Ah let me add also that SampleVal will be -512 to 512 and will need to remain an integer.
&#60;/p&#62;</description>
		</item>
		<item>
			<title>pyrohaz on "Help with float/fixed math and shortening interrupt time"</title>
			<link>http://forums.leaflabs.com/topic.php?id=11334#post-25599</link>
			<pubDate>Wed, 01 May 2013 19:13:22 +0000</pubDate>
			<dc:creator>pyrohaz</dc:creator>
			<guid isPermaLink="false">25599@http://forums.leaflabs.com/</guid>
			<description>&#60;p&#62;Hey,&#60;/p&#62;
&#60;p&#62;Sorry to drag back the subject but its becoming a bit of a bottleneck in my program!&#60;/p&#62;
&#60;p&#62;I'm using the filter found on MusicDSP (&#60;a href=&#34;http://www.musicdsp.org/showone.php?id=185&#34; rel=&#34;nofollow&#34;&#62;http://www.musicdsp.org/showone.php?id=185&#60;/a&#62;) and it sounds absolutely brilliant but i'm doing my filter calculations within an interrupt.&#60;/p&#62;
&#60;p&#62;My sample rate is at 36kHz, leaving about 27.8uS/interrupt trigger. Obviously i'm not wanting to use up all of that time in the interrupt or i'll never get anything else done!&#60;/p&#62;
&#60;p&#62;I've tested my code, it uses DDS to output a waveform with a 9bit phase accumulator and with the filter calculations disabled (using an if(filteroff = 1) to turn on/off the calculations) the code takes 4.5uS. This is fine as the interrupt will exit leaving about 23uS of processing time before another interrupt is fired. This is long enough to turn a couple of LED's on or off/read a few inputs.&#60;/p&#62;
&#60;p&#62;Of that 4.5uS, 1.6uS of that seems to be writing to the PWM outputs. i'm using pseudo 16bit PWM by combining two 8 bit PWM's and doing some bitshifts/and'ing to send the correct values to each output. Could this time be shortened with direct manipulation (quite like on arduino, using OCRxA/B)?&#60;/p&#62;
&#60;p&#62;Once the filter is activated (filteroff = 0), the interrupt time hikes up to 14.3uS which is immense! As you can probably tell, that only leaves me ~8uS of processor time between interrupts with ain't too much!&#60;/p&#62;
&#60;p&#62;After doing a simple test on the maple, I found that int math is 7.1x faster than float math and 9.6x faster than double math. Precision is not a massive problem here as i'm only using &#34;relative&#34; cut off frequencies instead of actual values (for example 200Hz).&#60;/p&#62;
&#60;p&#62;The filter code within the interrupt is:&#60;br /&#62;
FilterVariable1 = OneMinusRC*FilterVariable1 - Cutoff*FilterVariable2 + Cutoff*SampleVal;&#60;br /&#62;
FilterVariable2 = OneMinusRC*FilterVariable2 + Cutoff*FilterVariable1;&#60;br /&#62;
PWMWriteVar = FilterVariable2;&#60;/p&#62;
&#60;p&#62;OneMinusRC is calculated within the main loop to save on interrupt time, along with the cutoff. &#60;/p&#62;
&#60;p&#62;The calculations for these are:&#60;br /&#62;
  Cutoff = 6.3*FilterValue/SampleRate;&#60;br /&#62;
  Resonance = pow(0.5, ((256/ResV)+32)/16);&#60;br /&#62;
  OneMinusRC = (1-Resonance*Cutoff);&#60;/p&#62;
&#60;p&#62;Where filter value is my cutoff frequency and sample rate is 36k. ResV is the value used for resonance.&#60;/p&#62;
&#60;p&#62;As you can see, i've already reduced the accuracy by using the small angle approximation (tan(O) = O at small angles), and even pi has been reduced in accuracy from 6.282 to 6.3.&#60;/p&#62;
&#60;p&#62;How will I go about converting this to int math? If I could manage this, it would drastically reduce the interrupt time and allow me to add many more effects!&#60;/p&#62;
&#60;p&#62;Cheers,&#60;br /&#62;
Harris
&#60;/p&#62;</description>
		</item>

	</channel>
</rss>
