<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Research: Math, Computing and MRI &#187; Matlab</title>
	<atom:link href="http://mri.brechmos.org/category/matlab/feed/" rel="self" type="application/rss+xml" />
	<link>http://mri.brechmos.org</link>
	<description>by Craig Jones</description>
	<lastBuildDate>Mon, 07 Jun 2010 01:52:20 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>writeanalyze in Matlab</title>
		<link>http://mri.brechmos.org/2010/02/08/writeanalyze-in-matlab/</link>
		<comments>http://mri.brechmos.org/2010/02/08/writeanalyze-in-matlab/#comments</comments>
		<pubDate>Mon, 08 Feb 2010 14:57:23 +0000</pubDate>
		<dc:creator>craig</dc:creator>
				<category><![CDATA[MRI]]></category>
		<category><![CDATA[Matlab]]></category>
		<category><![CDATA[Code]]></category>

		<guid isPermaLink="false">http://mri.brechmos.org/?p=565</guid>
		<description><![CDATA[	
	<span class="Z3988" title="ctx_ver=Z39.88-2004&amp;rft_val_fmt=info%3Aofi%2Ffmt%3Akev%3Amtx%3Adc&amp;rfr_id=info%3Asid%2Focoins.info%3Agenerator&amp;rft.title=writeanalyze+in+Matlab&amp;rft.aulast=Jones&amp;rft.aufirst=Craig&amp;rft.subject=MRI&amp;rft.subject=Matlab&amp;rft.source=Research%3A+Math%2C+Computing+and+MRI&amp;rft.date=2010-02-08&amp;rft.type=blogPost&amp;rft.format=text&amp;rft.identifier=http://mri.brechmos.org/2010/02/08/writeanalyze-in-matlab/&amp;rft.language=English"></span>
I am always look for different MRI file readers and writers for the myriad of formats that we use in MRI research.  One of the relatively simple and common ones is the Analyze fileformat.  Some of the large packages have writers  (e.g., SPM) but I am typically wanting to do my own [...]]]></description>
			<content:encoded><![CDATA[	
	<span class="Z3988" title="ctx_ver=Z39.88-2004&amp;rft_val_fmt=info%3Aofi%2Ffmt%3Akev%3Amtx%3Adc&amp;rfr_id=info%3Asid%2Focoins.info%3Agenerator&amp;rft.title=writeanalyze+in+Matlab&amp;rft.aulast=Jones&amp;rft.aufirst=Craig&amp;rft.subject=MRI&amp;rft.subject=Matlab&amp;rft.source=Research%3A+Math%2C+Computing+and+MRI&amp;rft.date=2010-02-08&amp;rft.type=blogPost&amp;rft.format=text&amp;rft.identifier=http://mri.brechmos.org/2010/02/08/writeanalyze-in-matlab/&amp;rft.language=English"></span>
<p>I am always look for different MRI file readers and writers for the myriad of formats that we use in MRI research.  One of the relatively simple and common ones is the Analyze fileformat.  Some of the large packages have writers  (e.g., SPM) but I am typically wanting to do my own small processing and then write out the data.  So, I wrote up my own writeanalyze.m function.  It will do the basic formatting though the offsets etc don&#8217;t work.  Try it out but I can&#8217;t guarantee anything.</p>
<div class="codecolorer-container matlab geshi" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:99%;height:300px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br />19<br />20<br />21<br />22<br />23<br />24<br />25<br />26<br />27<br />28<br />29<br />30<br />31<br />32<br />33<br />34<br />35<br />36<br />37<br />38<br />39<br />40<br />41<br />42<br />43<br />44<br />45<br />46<br />47<br />48<br />49<br />50<br />51<br />52<br />53<br />54<br />55<br />56<br />57<br />58<br />59<br />60<br />61<br />62<br />63<br />64<br />65<br />66<br />67<br />68<br />69<br />70<br />71<br />72<br />73<br />74<br />75<br />76<br />77<br />78<br />79<br />80<br />81<br />82<br />83<br />84<br />85<br />86<br />87<br />88<br />89<br />90<br />91<br />92<br />93<br />94<br />95<br />96<br />97<br />98<br />99<br />100<br />101<br />102<br />103<br />104<br /></div></td><td><div class="matlab codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #0000FF;">function</span> <span style="color: #080;">&#91;</span><span style="color: #080;">&#93;</span> = writeanalyze<span style="color: #080;">&#40;</span>fname, data, ftype<span style="color: #080;">&#41;</span><br />
<br />
<span style="color: #0000FF;">if</span><span style="color: #080;">&#40;</span> <a href="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/nargin.html"><span style="color: #0000FF;">nargin</span></a> == <span style="color: #33f;">2</span> <span style="color: #080;">&#41;</span><br />
&nbsp; &nbsp; ftype = <span style="color:#A020F0;">'int16'</span>;<br />
<span style="color: #0000FF;">end</span><br />
<br />
<span style="color: #0000FF;">if</span><span style="color: #080;">&#40;</span> <a href="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/strcmp.html"><span style="color: #0000FF;">strcmp</span></a><span style="color: #080;">&#40;</span> ftype, <span style="color:#A020F0;">'int16'</span> <span style="color: #080;">&#41;</span> == <span style="color: #33f;">1</span> <span style="color: #080;">&#41;</span><br />
&nbsp; &nbsp; file_type = <span style="color: #33f;">4</span>; bpp = <span style="color: #33f;">16</span>;<br />
<span style="color: #0000FF;">elseif</span><span style="color: #080;">&#40;</span> <a href="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/strcmp.html"><span style="color: #0000FF;">strcmp</span></a><span style="color: #080;">&#40;</span> ftype, <span style="color:#A020F0;">'uint16'</span> <span style="color: #080;">&#41;</span> == <span style="color: #33f;">1</span> <span style="color: #080;">&#41;</span><br />
&nbsp; &nbsp; file_type = <span style="color: #33f;">4</span>; bpp = <span style="color: #33f;">16</span>;<br />
<span style="color: #0000FF;">elseif</span><span style="color: #080;">&#40;</span> <a href="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/strcmp.html"><span style="color: #0000FF;">strcmp</span></a><span style="color: #080;">&#40;</span> ftype, <span style="color:#A020F0;">'int32'</span> <span style="color: #080;">&#41;</span> == <span style="color: #33f;">1</span> <span style="color: #080;">&#41;</span><br />
&nbsp; &nbsp; file_type = <span style="color: #33f;">8</span>; bpp = <span style="color: #33f;">32</span>;<br />
<span style="color: #0000FF;">elseif</span><span style="color: #080;">&#40;</span> <a href="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/strcmp.html"><span style="color: #0000FF;">strcmp</span></a><span style="color: #080;">&#40;</span> ftype, <span style="color:#A020F0;">'float'</span> <span style="color: #080;">&#41;</span> == <span style="color: #33f;">1</span> <span style="color: #080;">&#41;</span><br />
&nbsp; &nbsp; file_type = <span style="color: #33f;">16</span>; bpp = <span style="color: #33f;">32</span>;<br />
<span style="color: #0000FF;">elseif</span><span style="color: #080;">&#40;</span> <a href="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/strcmp.html"><span style="color: #0000FF;">strcmp</span></a><span style="color: #080;">&#40;</span> ftype, <span style="color:#A020F0;">'double'</span> <span style="color: #080;">&#41;</span> == <span style="color: #33f;">1</span> <span style="color: #080;">&#41;</span><br />
&nbsp; &nbsp; file_type = <span style="color: #33f;">64</span>; bpp = <span style="color: #33f;">64</span>;<br />
<span style="color: #0000FF;">else</span><br />
&nbsp; &nbsp; <a href="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/error.html"><span style="color: #0000FF;">error</span></a><span style="color: #080;">&#40;</span><a href="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/sprintf.html"><span style="color: #0000FF;">sprintf</span></a><span style="color: #080;">&#40;</span><span style="color:#A020F0;">'Unknown data type %s'</span>, ftype<span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>;<br />
<span style="color: #0000FF;">end</span><br />
<br />
fp = <a href="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/fopen.html"><span style="color: #0000FF;">fopen</span></a><span style="color: #080;">&#40;</span><span style="color: #080;">&#91;</span>fname <span style="color:#A020F0;">'.hdr'</span><span style="color: #080;">&#93;</span>, <span style="color:#A020F0;">'wb'</span>, <span style="color:#A020F0;">'b'</span><span style="color: #080;">&#41;</span>;<br />
<br />
<span style="color: #228B22;">%%</span><br />
<span style="color: #228B22;">%% &nbsp;Write the header_key part</span><br />
<span style="color: #228B22;">%%</span><br />
<a href="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/fwrite.html"><span style="color: #0000FF;">fwrite</span></a><span style="color: #080;">&#40;</span>fp, <span style="color: #33f;">348</span>, <span style="color:#A020F0;">'int32'</span><span style="color: #080;">&#41;</span>;<br />
<a href="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/fwrite.html"><span style="color: #0000FF;">fwrite</span></a><span style="color: #080;">&#40;</span>fp, <a href="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/repmat.html"><span style="color: #0000FF;">repmat</span></a><span style="color: #080;">&#40;</span><span style="color:#A020F0;">' '</span>, <span style="color: #33f;">1</span>, <span style="color: #33f;">10</span><span style="color: #080;">&#41;</span>, <span style="color:#A020F0;">'char'</span><span style="color: #080;">&#41;</span>;<br />
<a href="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/fwrite.html"><span style="color: #0000FF;">fwrite</span></a><span style="color: #080;">&#40;</span>fp, <a href="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/repmat.html"><span style="color: #0000FF;">repmat</span></a><span style="color: #080;">&#40;</span><span style="color:#A020F0;">' '</span>, <span style="color: #33f;">1</span>, <span style="color: #33f;">18</span><span style="color: #080;">&#41;</span>, <span style="color:#A020F0;">'char'</span><span style="color: #080;">&#41;</span>;<br />
<a href="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/fwrite.html"><span style="color: #0000FF;">fwrite</span></a><span style="color: #080;">&#40;</span>fp, <span style="color: #33f;">16384</span>, <span style="color:#A020F0;">'int32'</span><span style="color: #080;">&#41;</span>;<br />
<a href="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/fwrite.html"><span style="color: #0000FF;">fwrite</span></a><span style="color: #080;">&#40;</span>fp, <span style="color: #33f;">0</span>, <span style="color:#A020F0;">'int16'</span><span style="color: #080;">&#41;</span>;<br />
<a href="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/fwrite.html"><span style="color: #0000FF;">fwrite</span></a><span style="color: #080;">&#40;</span>fp, <span style="color:#A020F0;">'r '</span>, <span style="color:#A020F0;">'char'</span><span style="color: #080;">&#41;</span>;<br />
<br />
<br />
<span style="color: #228B22;">%%</span><br />
<span style="color: #228B22;">%% Write the image_dimension part.</span><br />
<span style="color: #228B22;">%%</span><br />
<a href="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/fwrite.html"><span style="color: #0000FF;">fwrite</span></a><span style="color: #080;">&#40;</span>fp, <a href="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/length.html"><span style="color: #0000FF;">length</span></a><span style="color: #080;">&#40;</span> <a href="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/size.html"><span style="color: #0000FF;">size</span></a><span style="color: #080;">&#40;</span>data<span style="color: #080;">&#41;</span> <span style="color: #080;">&#41;</span>, <span style="color:#A020F0;">'int16'</span><span style="color: #080;">&#41;</span>;<br />
<span style="color: #0000FF;">for</span> ii=<span style="color: #33f;">1</span>:<a href="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/length.html"><span style="color: #0000FF;">length</span></a><span style="color: #080;">&#40;</span> <a href="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/size.html"><span style="color: #0000FF;">size</span></a><span style="color: #080;">&#40;</span>data<span style="color: #080;">&#41;</span> <span style="color: #080;">&#41;</span><br />
&nbsp; &nbsp; <a href="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/fwrite.html"><span style="color: #0000FF;">fwrite</span></a><span style="color: #080;">&#40;</span>fp, <a href="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/size.html"><span style="color: #0000FF;">size</span></a><span style="color: #080;">&#40;</span>data,ii<span style="color: #080;">&#41;</span>, <span style="color:#A020F0;">'int16'</span><span style="color: #080;">&#41;</span>;<br />
<span style="color: #0000FF;">end</span><br />
<br />
<span style="color: #0000FF;">for</span> ii=<a href="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/length.html"><span style="color: #0000FF;">length</span></a><span style="color: #080;">&#40;</span> <a href="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/size.html"><span style="color: #0000FF;">size</span></a><span style="color: #080;">&#40;</span>data<span style="color: #080;">&#41;</span> <span style="color: #080;">&#41;</span>+<span style="color: #33f;">1</span>:<span style="color: #33f;">7</span><br />
&nbsp; &nbsp; <a href="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/fwrite.html"><span style="color: #0000FF;">fwrite</span></a><span style="color: #080;">&#40;</span>fp, <span style="color: #33f;">1</span>, <span style="color:#A020F0;">'int16'</span><span style="color: #080;">&#41;</span>;<br />
<span style="color: #0000FF;">end</span> <br />
<br />
<a href="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/fwrite.html"><span style="color: #0000FF;">fwrite</span></a><span style="color: #080;">&#40;</span>fp, <span style="color: #33f;">0</span>, <span style="color:#A020F0;">'int16'</span><span style="color: #080;">&#41;</span>; &nbsp;<span style="color: #228B22;">% unused 8</span><br />
<a href="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/fwrite.html"><span style="color: #0000FF;">fwrite</span></a><span style="color: #080;">&#40;</span>fp, <span style="color: #33f;">0</span>, <span style="color:#A020F0;">'int16'</span><span style="color: #080;">&#41;</span>; &nbsp;<span style="color: #228B22;">% unused 9</span><br />
<a href="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/fwrite.html"><span style="color: #0000FF;">fwrite</span></a><span style="color: #080;">&#40;</span>fp, <span style="color: #33f;">0</span>, <span style="color:#A020F0;">'int16'</span><span style="color: #080;">&#41;</span>; &nbsp;<span style="color: #228B22;">% unused 10</span><br />
<a href="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/fwrite.html"><span style="color: #0000FF;">fwrite</span></a><span style="color: #080;">&#40;</span>fp, <span style="color: #33f;">0</span>, <span style="color:#A020F0;">'int16'</span><span style="color: #080;">&#41;</span>; &nbsp;<span style="color: #228B22;">% unused 11</span><br />
<a href="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/fwrite.html"><span style="color: #0000FF;">fwrite</span></a><span style="color: #080;">&#40;</span>fp, <span style="color: #33f;">0</span>, <span style="color:#A020F0;">'int16'</span><span style="color: #080;">&#41;</span>; &nbsp;<span style="color: #228B22;">% unused 12</span><br />
<a href="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/fwrite.html"><span style="color: #0000FF;">fwrite</span></a><span style="color: #080;">&#40;</span>fp, <span style="color: #33f;">0</span>, <span style="color:#A020F0;">'int16'</span><span style="color: #080;">&#41;</span>; &nbsp;<span style="color: #228B22;">% unused 13</span><br />
<a href="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/fwrite.html"><span style="color: #0000FF;">fwrite</span></a><span style="color: #080;">&#40;</span>fp, <span style="color: #33f;">0</span>, <span style="color:#A020F0;">'int16'</span><span style="color: #080;">&#41;</span>; &nbsp;<span style="color: #228B22;">% unused 14</span><br />
<br />
<span style="color: #228B22;">% data type</span><br />
<a href="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/fwrite.html"><span style="color: #0000FF;">fwrite</span></a><span style="color: #080;">&#40;</span>fp, file_type, <span style="color:#A020F0;">'int16'</span><span style="color: #080;">&#41;</span>; &nbsp; <span style="color: #228B22;">% 4 = signed short</span><br />
<a href="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/fwrite.html"><span style="color: #0000FF;">fwrite</span></a><span style="color: #080;">&#40;</span>fp, bpp, <span style="color:#A020F0;">'int16'</span><span style="color: #080;">&#41;</span>; &nbsp; <span style="color: #228B22;">% bpp</span><br />
<a href="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/fwrite.html"><span style="color: #0000FF;">fwrite</span></a><span style="color: #080;">&#40;</span>fp, <span style="color: #33f;">0</span>, <span style="color:#A020F0;">'int16'</span><span style="color: #080;">&#41;</span>;<br />
<span style="color: #0000FF;">for</span> ii=<span style="color: #33f;">1</span>:<span style="color: #33f;">8</span><br />
&nbsp; &nbsp; <a href="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/fwrite.html"><span style="color: #0000FF;">fwrite</span></a><span style="color: #080;">&#40;</span>fp, <span style="color: #33f;">1.0</span>, <span style="color:#A020F0;">'float32'</span><span style="color: #080;">&#41;</span>;<br />
<span style="color: #0000FF;">end</span><br />
<a href="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/fwrite.html"><span style="color: #0000FF;">fwrite</span></a><span style="color: #080;">&#40;</span>fp, <span style="color: #33f;">0</span>, <span style="color:#A020F0;">'float32'</span><span style="color: #080;">&#41;</span>;<br />
<a href="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/fwrite.html"><span style="color: #0000FF;">fwrite</span></a><span style="color: #080;">&#40;</span>fp, <span style="color: #33f;">0</span>, <span style="color:#A020F0;">'float32'</span><span style="color: #080;">&#41;</span>; <span style="color: #228B22;">% funused 1</span><br />
<a href="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/fwrite.html"><span style="color: #0000FF;">fwrite</span></a><span style="color: #080;">&#40;</span>fp, <span style="color: #33f;">0</span>, <span style="color:#A020F0;">'float32'</span><span style="color: #080;">&#41;</span>; <span style="color: #228B22;">% funused 2</span><br />
<a href="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/fwrite.html"><span style="color: #0000FF;">fwrite</span></a><span style="color: #080;">&#40;</span>fp, <span style="color: #33f;">0</span>, <span style="color:#A020F0;">'float32'</span><span style="color: #080;">&#41;</span>; <span style="color: #228B22;">% funused 3</span><br />
<br />
<a href="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/fwrite.html"><span style="color: #0000FF;">fwrite</span></a><span style="color: #080;">&#40;</span>fp, <a href="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/max.html"><span style="color: #0000FF;">max</span></a><span style="color: #080;">&#40;</span>data<span style="color: #080;">&#40;</span>:<span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>, <span style="color:#A020F0;">'float32'</span><span style="color: #080;">&#41;</span>; <br />
<a href="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/fwrite.html"><span style="color: #0000FF;">fwrite</span></a><span style="color: #080;">&#40;</span>fp, <a href="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/min.html"><span style="color: #0000FF;">min</span></a><span style="color: #080;">&#40;</span>data<span style="color: #080;">&#40;</span>:<span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>, <span style="color:#A020F0;">'float32'</span><span style="color: #080;">&#41;</span>; <br />
<a href="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/fwrite.html"><span style="color: #0000FF;">fwrite</span></a><span style="color: #080;">&#40;</span>fp, <span style="color: #33f;">0</span>, <span style="color:#A020F0;">'float32'</span><span style="color: #080;">&#41;</span>; <br />
<a href="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/fwrite.html"><span style="color: #0000FF;">fwrite</span></a><span style="color: #080;">&#40;</span>fp, <span style="color: #33f;">0</span>, <span style="color:#A020F0;">'float32'</span><span style="color: #080;">&#41;</span>; <br />
<br />
<a href="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/fwrite.html"><span style="color: #0000FF;">fwrite</span></a><span style="color: #080;">&#40;</span>fp, <a href="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/round.html"><span style="color: #0000FF;">round</span></a><span style="color: #080;">&#40;</span><a href="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/max.html"><span style="color: #0000FF;">max</span></a><span style="color: #080;">&#40;</span>data<span style="color: #080;">&#40;</span>:<span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>, <span style="color:#A020F0;">'int32'</span><span style="color: #080;">&#41;</span>; &nbsp; <span style="color: #228B22;">% glmax</span><br />
<a href="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/fwrite.html"><span style="color: #0000FF;">fwrite</span></a><span style="color: #080;">&#40;</span>fp, <a href="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/round.html"><span style="color: #0000FF;">round</span></a><span style="color: #080;">&#40;</span><a href="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/min.html"><span style="color: #0000FF;">min</span></a><span style="color: #080;">&#40;</span>data<span style="color: #080;">&#40;</span>:<span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>, <span style="color:#A020F0;">'int32'</span><span style="color: #080;">&#41;</span>; &nbsp; <span style="color: #228B22;">% glmin</span><br />
<br />
<span style="color: #228B22;">%%</span><br />
<span style="color: #228B22;">%% &nbsp;Data history</span><br />
<span style="color: #228B22;">%%</span><br />
<a href="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/fwrite.html"><span style="color: #0000FF;">fwrite</span></a><span style="color: #080;">&#40;</span>fp, <a href="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/repmat.html"><span style="color: #0000FF;">repmat</span></a><span style="color: #080;">&#40;</span><span style="color:#A020F0;">' '</span>, <span style="color: #33f;">1</span>, <span style="color: #33f;">80</span><span style="color: #080;">&#41;</span>, <span style="color:#A020F0;">'char'</span><span style="color: #080;">&#41;</span>; <span style="color: #228B22;">% descrip</span><br />
<a href="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/fwrite.html"><span style="color: #0000FF;">fwrite</span></a><span style="color: #080;">&#40;</span>fp, <a href="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/repmat.html"><span style="color: #0000FF;">repmat</span></a><span style="color: #080;">&#40;</span><span style="color:#A020F0;">' '</span>, <span style="color: #33f;">1</span>, <span style="color: #33f;">24</span><span style="color: #080;">&#41;</span>, <span style="color:#A020F0;">'char'</span><span style="color: #080;">&#41;</span>; <span style="color: #228B22;">% aux_file</span><br />
<a href="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/fwrite.html"><span style="color: #0000FF;">fwrite</span></a><span style="color: #080;">&#40;</span>fp, <span style="color:#A020F0;">'3'</span>, <span style="color:#A020F0;">'char'</span><span style="color: #080;">&#41;</span>; <span style="color: #228B22;">% aux_file</span><br />
<a href="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/fwrite.html"><span style="color: #0000FF;">fwrite</span></a><span style="color: #080;">&#40;</span>fp, <a href="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/repmat.html"><span style="color: #0000FF;">repmat</span></a><span style="color: #080;">&#40;</span><span style="color:#A020F0;">' '</span>, <span style="color: #33f;">1</span>, <span style="color: #33f;">10</span><span style="color: #080;">&#41;</span>, <span style="color:#A020F0;">'char'</span><span style="color: #080;">&#41;</span>; <span style="color: #228B22;">% originator</span><br />
<a href="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/fwrite.html"><span style="color: #0000FF;">fwrite</span></a><span style="color: #080;">&#40;</span>fp, <a href="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/repmat.html"><span style="color: #0000FF;">repmat</span></a><span style="color: #080;">&#40;</span><span style="color:#A020F0;">' '</span>, <span style="color: #33f;">1</span>, <span style="color: #33f;">10</span><span style="color: #080;">&#41;</span>, <span style="color:#A020F0;">'char'</span><span style="color: #080;">&#41;</span>; <span style="color: #228B22;">% originator</span><br />
<a href="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/fwrite.html"><span style="color: #0000FF;">fwrite</span></a><span style="color: #080;">&#40;</span>fp, <a href="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/repmat.html"><span style="color: #0000FF;">repmat</span></a><span style="color: #080;">&#40;</span><span style="color:#A020F0;">' '</span>, <span style="color: #33f;">1</span>, <span style="color: #33f;">10</span><span style="color: #080;">&#41;</span>, <span style="color:#A020F0;">'char'</span><span style="color: #080;">&#41;</span>; <span style="color: #228B22;">% originator</span><br />
<a href="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/fwrite.html"><span style="color: #0000FF;">fwrite</span></a><span style="color: #080;">&#40;</span>fp, <a href="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/repmat.html"><span style="color: #0000FF;">repmat</span></a><span style="color: #080;">&#40;</span><span style="color:#A020F0;">' '</span>, <span style="color: #33f;">1</span>, <span style="color: #33f;">10</span><span style="color: #080;">&#41;</span>, <span style="color:#A020F0;">'char'</span><span style="color: #080;">&#41;</span>; <span style="color: #228B22;">% originator</span><br />
<a href="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/fwrite.html"><span style="color: #0000FF;">fwrite</span></a><span style="color: #080;">&#40;</span>fp, <a href="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/repmat.html"><span style="color: #0000FF;">repmat</span></a><span style="color: #080;">&#40;</span><span style="color:#A020F0;">' '</span>, <span style="color: #33f;">1</span>, <span style="color: #33f;">10</span><span style="color: #080;">&#41;</span>, <span style="color:#A020F0;">'char'</span><span style="color: #080;">&#41;</span>; <span style="color: #228B22;">% originator</span><br />
<a href="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/fwrite.html"><span style="color: #0000FF;">fwrite</span></a><span style="color: #080;">&#40;</span>fp, <a href="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/repmat.html"><span style="color: #0000FF;">repmat</span></a><span style="color: #080;">&#40;</span><span style="color:#A020F0;">' '</span>, <span style="color: #33f;">1</span>, <span style="color: #33f;">10</span><span style="color: #080;">&#41;</span>, <span style="color:#A020F0;">'char'</span><span style="color: #080;">&#41;</span>; <span style="color: #228B22;">% originator</span><br />
<a href="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/fwrite.html"><span style="color: #0000FF;">fwrite</span></a><span style="color: #080;">&#40;</span>fp, <a href="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/repmat.html"><span style="color: #0000FF;">repmat</span></a><span style="color: #080;">&#40;</span><span style="color:#A020F0;">' '</span>, <span style="color: #33f;">1</span>, <span style="color: #33f;">3</span><span style="color: #080;">&#41;</span>, <span style="color:#A020F0;">'char'</span><span style="color: #080;">&#41;</span>; <span style="color: #228B22;">% originator</span><br />
<br />
<a href="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/fwrite.html"><span style="color: #0000FF;">fwrite</span></a><span style="color: #080;">&#40;</span>fp, <span style="color: #33f;">0</span>, <span style="color:#A020F0;">'int32'</span><span style="color: #080;">&#41;</span>; <span style="color: #228B22;">% views</span><br />
<a href="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/fwrite.html"><span style="color: #0000FF;">fwrite</span></a><span style="color: #080;">&#40;</span>fp, <span style="color: #33f;">0</span>, <span style="color:#A020F0;">'int32'</span><span style="color: #080;">&#41;</span>; <span style="color: #228B22;">% vols_added</span><br />
<a href="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/fwrite.html"><span style="color: #0000FF;">fwrite</span></a><span style="color: #080;">&#40;</span>fp, <span style="color: #33f;">0</span>, <span style="color:#A020F0;">'int32'</span><span style="color: #080;">&#41;</span>; <span style="color: #228B22;">% start_fiedl</span><br />
<a href="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/fwrite.html"><span style="color: #0000FF;">fwrite</span></a><span style="color: #080;">&#40;</span>fp, <span style="color: #33f;">0</span>, <span style="color:#A020F0;">'int32'</span><span style="color: #080;">&#41;</span>; <span style="color: #228B22;">% field_skip</span><br />
<a href="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/fwrite.html"><span style="color: #0000FF;">fwrite</span></a><span style="color: #080;">&#40;</span>fp, <span style="color: #33f;">0</span>, <span style="color:#A020F0;">'int32'</span><span style="color: #080;">&#41;</span>; <span style="color: #228B22;">% omax</span><br />
<a href="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/fwrite.html"><span style="color: #0000FF;">fwrite</span></a><span style="color: #080;">&#40;</span>fp, <span style="color: #33f;">0</span>, <span style="color:#A020F0;">'int32'</span><span style="color: #080;">&#41;</span>; <span style="color: #228B22;">% omin</span><br />
<a href="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/fwrite.html"><span style="color: #0000FF;">fwrite</span></a><span style="color: #080;">&#40;</span>fp, <span style="color: #33f;">0</span>, <span style="color:#A020F0;">'int32'</span><span style="color: #080;">&#41;</span>; <span style="color: #228B22;">% small_max</span><br />
<a href="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/fwrite.html"><span style="color: #0000FF;">fwrite</span></a><span style="color: #080;">&#40;</span>fp, <span style="color: #33f;">0</span>, <span style="color:#A020F0;">'int32'</span><span style="color: #080;">&#41;</span>; <span style="color: #228B22;">% small_min</span><br />
<br />
<a href="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/fclose.html"><span style="color: #0000FF;">fclose</span></a><span style="color: #080;">&#40;</span>fp<span style="color: #080;">&#41;</span>;<br />
<br />
<span style="color: #228B22;">%%</span><br />
<span style="color: #228B22;">%% &nbsp;Write the data</span><br />
<span style="color: #228B22;">%%</span><br />
fp = <a href="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/fopen.html"><span style="color: #0000FF;">fopen</span></a><span style="color: #080;">&#40;</span><span style="color: #080;">&#91;</span>fname <span style="color:#A020F0;">'.img'</span><span style="color: #080;">&#93;</span>, <span style="color:#A020F0;">'wb'</span>, <span style="color:#A020F0;">'b'</span><span style="color: #080;">&#41;</span>;<br />
<a href="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/fwrite.html"><span style="color: #0000FF;">fwrite</span></a><span style="color: #080;">&#40;</span>fp, data, ftype<span style="color: #080;">&#41;</span>;<br />
<a href="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/fclose.html"><span style="color: #0000FF;">fclose</span></a><span style="color: #080;">&#40;</span>fp<span style="color: #080;">&#41;</span>;</div></td></tr></tbody></table></div>
]]></content:encoded>
			<wfw:commentRss>http://mri.brechmos.org/2010/02/08/writeanalyze-in-matlab/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Reading raw data in Matlab</title>
		<link>http://mri.brechmos.org/2010/02/01/reading-raw-data-in-matlab/</link>
		<comments>http://mri.brechmos.org/2010/02/01/reading-raw-data-in-matlab/#comments</comments>
		<pubDate>Tue, 02 Feb 2010 02:20:07 +0000</pubDate>
		<dc:creator>craig</dc:creator>
				<category><![CDATA[Computing]]></category>
		<category><![CDATA[Matlab]]></category>
		<category><![CDATA[Code]]></category>

		<guid isPermaLink="false">http://mri.brechmos.org/?p=555</guid>
		<description><![CDATA[	
	<span class="Z3988" title="ctx_ver=Z39.88-2004&amp;rft_val_fmt=info%3Aofi%2Ffmt%3Akev%3Amtx%3Adc&amp;rfr_id=info%3Asid%2Focoins.info%3Agenerator&amp;rft.title=Reading+raw+data+in+Matlab&amp;rft.aulast=Jones&amp;rft.aufirst=Craig&amp;rft.subject=Computing&amp;rft.subject=Matlab&amp;rft.source=Research%3A+Math%2C+Computing+and+MRI&amp;rft.date=2010-02-01&amp;rft.type=blogPost&amp;rft.format=text&amp;rft.identifier=http://mri.brechmos.org/2010/02/01/reading-raw-data-in-matlab/&amp;rft.language=English"></span>
One of the most common things I do in Matlab almost always involves reading in binary data.  For a few years I went through the typical fp=fopen&#40;'filename.dat'....  After typing the fopen, fread, reshape and fclose too many times, I finally made it into an all-in-one Matlab function called readraw&#40;&#41; which will do all the reading [...]]]></description>
			<content:encoded><![CDATA[	
	<span class="Z3988" title="ctx_ver=Z39.88-2004&amp;rft_val_fmt=info%3Aofi%2Ffmt%3Akev%3Amtx%3Adc&amp;rfr_id=info%3Asid%2Focoins.info%3Agenerator&amp;rft.title=Reading+raw+data+in+Matlab&amp;rft.aulast=Jones&amp;rft.aufirst=Craig&amp;rft.subject=Computing&amp;rft.subject=Matlab&amp;rft.source=Research%3A+Math%2C+Computing+and+MRI&amp;rft.date=2010-02-01&amp;rft.type=blogPost&amp;rft.format=text&amp;rft.identifier=http://mri.brechmos.org/2010/02/01/reading-raw-data-in-matlab/&amp;rft.language=English"></span>
<p>One of the most common things I do in Matlab almost always involves reading in binary data.  For a few years I went through the typical <code class="codecolorer matlab geshi"><span class="matlab">fp=<a href="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/fopen.html"><span style="color: #0000FF;">fopen</span></a><span style="color: #080;">&#40;</span><span style="color:#A020F0;">'filename.dat'</span><span style="color: #080;">...</span></span></code>.  After typing the fopen, fread, reshape and fclose too many times, I finally made it into an all-in-one Matlab function called <code class="codecolorer matlab geshi"><span class="matlab">readraw<span style="color: #080;">&#40;</span><span style="color: #080;">&#41;</span></span></code> which will do all the reading and reformatting in one function call.</p>
<div class="codecolorer-container matlab geshi" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:99%;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br /></div></td><td><div class="matlab codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #0000FF;">function</span> <span style="color: #080;">&#91;</span>d<span style="color: #080;">&#93;</span> = readraw<span style="color: #080;">&#40;</span>filename, <a href="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/type.html"><span style="color: #0000FF;">type</span></a>, ds, endian<span style="color: #080;">&#41;</span><br />
<br />
<span style="color: #0000FF;">if</span><span style="color: #080;">&#40;</span> <a href="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/nargin.html"><span style="color: #0000FF;">nargin</span></a> == <span style="color: #33f;">3</span> <span style="color: #080;">&#41;</span><br />
&nbsp; &nbsp; endian = <span style="color:#A020F0;">'b'</span>;<br />
<span style="color: #0000FF;">end</span><br />
<br />
fp=<a href="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/fopen.html"><span style="color: #0000FF;">fopen</span></a><span style="color: #080;">&#40;</span>filename, <span style="color:#A020F0;">'rb'</span>, endian<span style="color: #080;">&#41;</span>;<br />
d = <a href="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/fread.html"><span style="color: #0000FF;">fread</span></a><span style="color: #080;">&#40;</span>fp, <a href="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/prod.html"><span style="color: #0000FF;">prod</span></a><span style="color: #080;">&#40;</span>ds<span style="color: #080;">&#41;</span>, <a href="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/type.html"><span style="color: #0000FF;">type</span></a><span style="color: #080;">&#41;</span>;<br />
<a href="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/fclose.html"><span style="color: #0000FF;">fclose</span></a><span style="color: #080;">&#40;</span>fp<span style="color: #080;">&#41;</span>;<br />
<br />
d = <a href="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/reshape.html"><span style="color: #0000FF;">reshape</span></a><span style="color: #080;">&#40;</span>d, ds<span style="color: #080;">&#41;</span>;</div></td></tr></tbody></table></div>
<p>There isn&#8217;t much magic here, just a simple idea that I use almost daily.</p>
]]></content:encoded>
			<wfw:commentRss>http://mri.brechmos.org/2010/02/01/reading-raw-data-in-matlab/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Anisotropic Diffusion Image Filtering in MRI</title>
		<link>http://mri.brechmos.org/2010/01/19/anisotropic-diffusion-image-filtering-in-mri/</link>
		<comments>http://mri.brechmos.org/2010/01/19/anisotropic-diffusion-image-filtering-in-mri/#comments</comments>
		<pubDate>Wed, 20 Jan 2010 03:25:51 +0000</pubDate>
		<dc:creator>craig</dc:creator>
				<category><![CDATA[MRI]]></category>
		<category><![CDATA[Matlab]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[Code]]></category>

		<guid isPermaLink="false">http://mri.brechmos.org/?p=432</guid>
		<description><![CDATA[	
	<span class="Z3988" title="ctx_ver=Z39.88-2004&amp;rft_val_fmt=info%3Aofi%2Ffmt%3Akev%3Amtx%3Adc&amp;rfr_id=info%3Asid%2Focoins.info%3Agenerator&amp;rft.title=Anisotropic+Diffusion+Image+Filtering+in+MRI&amp;rft.aulast=Jones&amp;rft.aufirst=Craig&amp;rft.subject=MRI&amp;rft.subject=Matlab&amp;rft.subject=Python&amp;rft.source=Research%3A+Math%2C+Computing+and+MRI&amp;rft.date=2010-01-19&amp;rft.type=blogPost&amp;rft.format=text&amp;rft.identifier=http://mri.brechmos.org/2010/01/19/anisotropic-diffusion-image-filtering-in-mri/&amp;rft.language=English"></span>
Background
Magnetic resonance imaging has the tradeoff of signal-to-noise vs time vs resolution.  You can only choose two. For some applications it may be better to get higher temporal and spatial resolution than signal-to-noise and then one may do some spatial filtering.  Simple filtering would be applying a median filter or Gaussian smoothing over the image [...]]]></description>
			<content:encoded><![CDATA[	
	<span class="Z3988" title="ctx_ver=Z39.88-2004&amp;rft_val_fmt=info%3Aofi%2Ffmt%3Akev%3Amtx%3Adc&amp;rfr_id=info%3Asid%2Focoins.info%3Agenerator&amp;rft.title=Anisotropic+Diffusion+Image+Filtering+in+MRI&amp;rft.aulast=Jones&amp;rft.aufirst=Craig&amp;rft.subject=MRI&amp;rft.subject=Matlab&amp;rft.subject=Python&amp;rft.source=Research%3A+Math%2C+Computing+and+MRI&amp;rft.date=2010-01-19&amp;rft.type=blogPost&amp;rft.format=text&amp;rft.identifier=http://mri.brechmos.org/2010/01/19/anisotropic-diffusion-image-filtering-in-mri/&amp;rft.language=English"></span>
<h2>Background</h2>
<p>Magnetic resonance imaging has the tradeoff of signal-to-noise vs time vs resolution.  You can only choose two. For some applications it may be better to get higher temporal and spatial resolution than signal-to-noise and then one may do some spatial filtering.  Simple filtering would be applying a median filter or Gaussian smoothing over the image (or volume).  But there are better techniques.</p>
<h2>Smarter Filtering</h2>
<p>One option for a smarter filter is the anisotropic diffusion filter which was first introduced to MRI in 1992 <a href="#foot_0" name="foot_src_0">[1]</a>.  The basic idea is given a central voxel in a kernel and an estimation of noise the surrounding voxels are included in the smoothing based on the difference in signal to the central voxel relative to the estimation of noise.</p>
<p>I wrote a paper on this technique applied to multi-echo data <a href="#foot_1" name="foot_src_1">[2]</a>.</p>
<p>There is a fine line between filtering and over-filtering.  That is a whole separate discussion.</p>
<table>
<caption> The images below are a single slice of an MPRAGE image without filtering (left) and with anisotropic diffusion filtering (right).  The bottom set are just zoomed in versions of the top.  The filtered data might be slightly over filtered but was done to show the affect of the filter.<br />
</caption>
<tbody>
<tr>
<td><a rel="attachment wp-att-468" href="http://mri.brechmos.org/2010/01/19/anisotropic-diffusion-image-filtering-in-mri/mprage_noaniso-2/"><img class="alignnone size-medium wp-image-468" title="mprage_noaniso" src="http://mri.brechmos.org/wp-content/uploads/2010/01/19/anisotropic-diffusion-image-filtering-in-mri/mprage_noaniso1-450x450.png" alt="" width="350" /></a></td>
<td><a rel="attachment wp-att-466" href="http://mri.brechmos.org/2010/01/19/anisotropic-diffusion-image-filtering-in-mri/mprage_aniso-2/"><img class="alignnone size-medium wp-image-466" title="mprage_aniso" src="http://mri.brechmos.org/wp-content/uploads/2010/01/19/anisotropic-diffusion-image-filtering-in-mri/mprage_aniso1-449x450.png" alt="" width="350" /></a></td>
</tr>
<tr>
<td><a rel="attachment wp-att-469" href="http://mri.brechmos.org/2010/01/19/anisotropic-diffusion-image-filtering-in-mri/mprage_noaniso_zoomed-2/"><img class="alignnone size-medium wp-image-469" title="mprage_noaniso_zoomed" src="http://mri.brechmos.org/wp-content/uploads/2010/01/19/anisotropic-diffusion-image-filtering-in-mri/mprage_noaniso_zoomed1-277x450.png" alt="" width="277" height="450" /></a></td>
<td><a rel="attachment wp-att-467" href="http://mri.brechmos.org/2010/01/19/anisotropic-diffusion-image-filtering-in-mri/mprage_aniso_zoomed-2/"><img class="alignnone size-medium wp-image-467" title="mprage_aniso_zoomed" src="http://mri.brechmos.org/wp-content/uploads/2010/01/19/anisotropic-diffusion-image-filtering-in-mri/mprage_aniso_zoomed1-275x450.png" alt="" width="275" height="450" /></a></td>
</tr>
</tbody>
</table>
<h2>Code</h2>
<h3>Matlab</h3>
<p>The version below is for a 3D dataset:</p>
<div class="codecolorer-container matlab geshi" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:99%;height:300px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br />19<br />20<br />21<br />22<br />23<br />24<br /></div></td><td><div class="matlab codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #0000FF;">function</span> <span style="color: #080;">&#91;</span>filt_vol<span style="color: #080;">&#93;</span> = aniso3d<span style="color: #080;">&#40;</span>orig_vol, kappa, niters<span style="color: #080;">&#41;</span><br />
<br />
<span style="color: #0000FF;">if</span><span style="color: #080;">&#40;</span> <a href="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/nargin.html"><span style="color: #0000FF;">nargin</span></a> &amp;lt; <span style="color: #33f;">3</span> <span style="color: #080;">&#41;</span><br />
<a href="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/error.html"><span style="color: #0000FF;">error</span></a><span style="color: #080;">&#40;</span><span style="color:#A020F0;">'aniso3d: Need more parameters'</span><span style="color: #080;">&#41;</span>;<br />
<span style="color: #0000FF;">end</span><br />
<br />
filt_vol = orig_vol;<br />
<br />
<span style="color: #0000FF;">for</span> iters = <span style="color: #33f;">1</span>:niters<br />
<br />
&nbsp; &nbsp; dE = convn<span style="color: #080;">&#40;</span>filt_vol, <span style="color: #080;">&#91;</span><span style="color: #33f;">0</span> -<span style="color: #33f;">1</span> <span style="color: #33f;">1</span><span style="color: #080;">&#93;</span>, <span style="color:#A020F0;">'full'</span><span style="color: #080;">&#41;</span>; dE=dE<span style="color: #080;">&#40;</span>:,<span style="color: #33f;">2</span>:ncols<span style="color: #080;">&#40;</span>dE<span style="color: #080;">&#41;</span>-<span style="color: #33f;">1</span>,:<span style="color: #080;">&#41;</span>;<br />
&nbsp; &nbsp; dW = convn<span style="color: #080;">&#40;</span>filt_vol, <span style="color: #080;">&#91;</span>-<span style="color: #33f;">1</span> <span style="color: #33f;">1</span> <span style="color: #33f;">0</span><span style="color: #080;">&#93;</span>, <span style="color:#A020F0;">'full'</span><span style="color: #080;">&#41;</span>; dW=dW<span style="color: #080;">&#40;</span>:,<span style="color: #33f;">2</span>:ncols<span style="color: #080;">&#40;</span>dW<span style="color: #080;">&#41;</span>-<span style="color: #33f;">1</span>,:<span style="color: #080;">&#41;</span>;<br />
&nbsp; &nbsp; dN = convn<span style="color: #080;">&#40;</span>filt_vol, <span style="color: #080;">&#91;</span><span style="color: #33f;">0</span>; -<span style="color: #33f;">1</span>; <span style="color: #33f;">1</span><span style="color: #080;">&#93;</span>, <span style="color:#A020F0;">'full'</span><span style="color: #080;">&#41;</span>; dN=dN<span style="color: #080;">&#40;</span><span style="color: #33f;">2</span>:nrows<span style="color: #080;">&#40;</span>dN<span style="color: #080;">&#41;</span>-<span style="color: #33f;">1</span>,:,:<span style="color: #080;">&#41;</span>;<br />
&nbsp; &nbsp; dS = convn<span style="color: #080;">&#40;</span>filt_vol, <span style="color: #080;">&#91;</span>-<span style="color: #33f;">1</span>; <span style="color: #33f;">1</span>; <span style="color: #33f;">0</span><span style="color: #080;">&#93;</span>, <span style="color:#A020F0;">'full'</span><span style="color: #080;">&#41;</span>; dS=dS<span style="color: #080;">&#40;</span><span style="color: #33f;">2</span>:nrows<span style="color: #080;">&#40;</span>dS<span style="color: #080;">&#41;</span>-<span style="color: #33f;">1</span>,:,:<span style="color: #080;">&#41;</span>;<br />
&nbsp; &nbsp; kernel = <a href="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/zeros.html"><span style="color: #0000FF;">zeros</span></a><span style="color: #080;">&#40;</span><span style="color: #33f;">1</span>,<span style="color: #33f;">1</span>,<span style="color: #33f;">3</span><span style="color: #080;">&#41;</span>; kernel<span style="color: #080;">&#40;</span><span style="color: #33f;">2</span><span style="color: #080;">&#41;</span> = -<span style="color: #33f;">1</span>; kernel<span style="color: #080;">&#40;</span><span style="color: #33f;">3</span><span style="color: #080;">&#41;</span> = <span style="color: #33f;">1</span>;<br />
&nbsp; &nbsp; dU = convn<span style="color: #080;">&#40;</span>filt_vol, kernel, <span style="color:#A020F0;">'full'</span><span style="color: #080;">&#41;</span>; dU=dU<span style="color: #080;">&#40;</span>:,:,<span style="color: #33f;">2</span>:<a href="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/size.html"><span style="color: #0000FF;">size</span></a><span style="color: #080;">&#40;</span>dU,<span style="color: #33f;">3</span><span style="color: #080;">&#41;</span>-<span style="color: #33f;">1</span><span style="color: #080;">&#41;</span>;<br />
&nbsp; &nbsp; kernel = <a href="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/zeros.html"><span style="color: #0000FF;">zeros</span></a><span style="color: #080;">&#40;</span><span style="color: #33f;">1</span>,<span style="color: #33f;">1</span>,<span style="color: #33f;">3</span><span style="color: #080;">&#41;</span>; kernel<span style="color: #080;">&#40;</span><span style="color: #33f;">1</span><span style="color: #080;">&#41;</span> = -<span style="color: #33f;">1</span>; kernel<span style="color: #080;">&#40;</span><span style="color: #33f;">2</span><span style="color: #080;">&#41;</span> = <span style="color: #33f;">1</span>;<br />
&nbsp; &nbsp; dD = convn<span style="color: #080;">&#40;</span>filt_vol, kernel, <span style="color:#A020F0;">'full'</span><span style="color: #080;">&#41;</span>; dD=dD<span style="color: #080;">&#40;</span>:,:,<span style="color: #33f;">2</span>:<a href="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/size.html"><span style="color: #0000FF;">size</span></a><span style="color: #080;">&#40;</span>dD,<span style="color: #33f;">3</span><span style="color: #080;">&#41;</span>-<span style="color: #33f;">1</span><span style="color: #080;">&#41;</span>;<br />
<br />
&nbsp; &nbsp; filt_vol = filt_vol +  <span style="color: #080;">...</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #33f;">3</span>/<span style="color: #33f;">28</span> * <span style="color: #080;">&#40;</span><span style="color: #080;">&#40;</span><a href="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/double.html"><span style="color: #0000FF;">double</span></a><span style="color: #080;">&#40;</span><a href="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/exp.html"><span style="color: #0000FF;">exp</span></a><span style="color: #080;">&#40;</span>- <span style="color: #080;">&#40;</span><a href="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/abs.html"><span style="color: #0000FF;">abs</span></a><span style="color: #080;">&#40;</span>dE<span style="color: #080;">&#41;</span> / kappa<span style="color: #080;">&#41;</span>.^<span style="color: #33f;">2</span> <span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span> .* <a href="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/double.html"><span style="color: #0000FF;">double</span></a><span style="color: #080;">&#40;</span>dE<span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span> - <span style="color: #080;">&#40;</span><a href="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/double.html"><span style="color: #0000FF;">double</span></a><span style="color: #080;">&#40;</span><a href="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/exp.html"><span style="color: #0000FF;">exp</span></a><span style="color: #080;">&#40;</span>- <span style="color: #080;">&#40;</span><a href="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/abs.html"><span style="color: #0000FF;">abs</span></a><span style="color: #080;">&#40;</span>dW<span style="color: #080;">&#41;</span> / kappa<span style="color: #080;">&#41;</span>.^<span style="color: #33f;">2</span> <span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span> .* <a href="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/double.html"><span style="color: #0000FF;">double</span></a><span style="color: #080;">&#40;</span>dW<span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span> + <span style="color: #080;">...</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #33f;">3</span>/<span style="color: #33f;">28</span> * <span style="color: #080;">&#40;</span><span style="color: #080;">&#40;</span><a href="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/double.html"><span style="color: #0000FF;">double</span></a><span style="color: #080;">&#40;</span><a href="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/exp.html"><span style="color: #0000FF;">exp</span></a><span style="color: #080;">&#40;</span>- <span style="color: #080;">&#40;</span><a href="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/abs.html"><span style="color: #0000FF;">abs</span></a><span style="color: #080;">&#40;</span>dN<span style="color: #080;">&#41;</span> / kappa<span style="color: #080;">&#41;</span>.^<span style="color: #33f;">2</span> <span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span> .* <a href="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/double.html"><span style="color: #0000FF;">double</span></a><span style="color: #080;">&#40;</span>dN<span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span> - <span style="color: #080;">&#40;</span><a href="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/double.html"><span style="color: #0000FF;">double</span></a><span style="color: #080;">&#40;</span><a href="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/exp.html"><span style="color: #0000FF;">exp</span></a><span style="color: #080;">&#40;</span>- <span style="color: #080;">&#40;</span><a href="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/abs.html"><span style="color: #0000FF;">abs</span></a><span style="color: #080;">&#40;</span>dS<span style="color: #080;">&#41;</span> / kappa<span style="color: #080;">&#41;</span>.^<span style="color: #33f;">2</span> <span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span> .* &nbsp;<a href="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/double.html"><span style="color: #0000FF;">double</span></a><span style="color: #080;">&#40;</span>dS<span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span> + <span style="color: #080;">...</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #33f;">1</span>/<span style="color: #33f;">28</span> * <span style="color: #080;">&#40;</span><span style="color: #080;">&#40;</span><a href="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/double.html"><span style="color: #0000FF;">double</span></a><span style="color: #080;">&#40;</span><a href="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/exp.html"><span style="color: #0000FF;">exp</span></a><span style="color: #080;">&#40;</span>- <span style="color: #080;">&#40;</span><a href="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/abs.html"><span style="color: #0000FF;">abs</span></a><span style="color: #080;">&#40;</span>dU<span style="color: #080;">&#41;</span> / kappa<span style="color: #080;">&#41;</span>.^<span style="color: #33f;">2</span> <span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span> .* <a href="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/double.html"><span style="color: #0000FF;">double</span></a><span style="color: #080;">&#40;</span>dU<span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span> - <span style="color: #080;">&#40;</span><a href="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/double.html"><span style="color: #0000FF;">double</span></a><span style="color: #080;">&#40;</span><a href="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/exp.html"><span style="color: #0000FF;">exp</span></a><span style="color: #080;">&#40;</span>- <span style="color: #080;">&#40;</span><a href="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/abs.html"><span style="color: #0000FF;">abs</span></a><span style="color: #080;">&#40;</span>dD<span style="color: #080;">&#41;</span> / kappa<span style="color: #080;">&#41;</span>.^<span style="color: #33f;">2</span> <span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span> .* <a href="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/double.html"><span style="color: #0000FF;">double</span></a><span style="color: #080;">&#40;</span>dD<span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>;<br />
<span style="color: #0000FF;">end</span></div></td></tr></tbody></table></div>
<p>For 4D data one can also smooth across the 4th dimension (whether it is time, diffusion etc).</p>
<div class="codecolorer-container matlab geshi" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:99%;height:300px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br />19<br />20<br />21<br />22<br />23<br />24<br />25<br />26<br />27<br />28<br />29<br />30<br />31<br />32<br />33<br />34<br />35<br />36<br />37<br />38<br />39<br />40<br />41<br />42<br />43<br />44<br />45<br />46<br />47<br />48<br />49<br />50<br />51<br /></div></td><td><div class="matlab codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #0000FF;">function</span> <span style="color: #080;">&#91;</span>filt_vol<span style="color: #080;">&#93;</span> = aniso3d_chan<span style="color: #080;">&#40;</span>orig_vol, kappa, niters<span style="color: #080;">&#41;</span><br />
<span style="color: #228B22;">%</span><br />
<span style="color: #228B22;">% &nbsp;aniso3d_chan - Run the anisotropic diffusion filter in 3D</span><br />
<span style="color: #228B22;">% &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; and over the multiple channels.</span><br />
<span style="color: #228B22;">%</span><br />
<br />
<span style="color: #0000FF;">if</span><span style="color: #080;">&#40;</span> <a href="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/nargin.html"><span style="color: #0000FF;">nargin</span></a> &amp;lt; <span style="color: #33f;">3</span> <span style="color: #080;">&#41;</span><br />
<a href="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/error.html"><span style="color: #0000FF;">error</span></a><span style="color: #080;">&#40;</span><span style="color:#A020F0;">'aniso3d: Need more parameters'</span><span style="color: #080;">&#41;</span>;<br />
<span style="color: #0000FF;">end</span><br />
<br />
filt_vol = float<span style="color: #080;">&#40;</span><a href="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/squeeze.html"><span style="color: #0000FF;">squeeze</span></a><span style="color: #080;">&#40;</span>orig_vol<span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>;<br />
<br />
<span style="color: #0000FF;">for</span> iters = <span style="color: #33f;">1</span>:niters<br />
&nbsp; &nbsp; dE = convn<span style="color: #080;">&#40;</span>filt_vol, <span style="color: #080;">&#91;</span><span style="color: #33f;">0</span> -<span style="color: #33f;">1</span> <span style="color: #33f;">1</span><span style="color: #080;">&#93;</span>, <span style="color:#A020F0;">'full'</span><span style="color: #080;">&#41;</span>; dE=dE<span style="color: #080;">&#40;</span>:,<span style="color: #33f;">2</span>:ncols<span style="color: #080;">&#40;</span>dE<span style="color: #080;">&#41;</span>-<span style="color: #33f;">1</span>,:,:<span style="color: #080;">&#41;</span>;<br />
&nbsp; &nbsp; cE = <a href="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/repmat.html"><span style="color: #0000FF;">repmat</span></a><span style="color: #080;">&#40;</span><a href="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/sqrt.html"><span style="color: #0000FF;">sqrt</span></a><span style="color: #080;">&#40;</span><a href="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/sum.html"><span style="color: #0000FF;">sum</span></a><span style="color: #080;">&#40;</span>dE.^<span style="color: #33f;">2</span>, <span style="color: #33f;">4</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>, <span style="color: #080;">&#91;</span><span style="color: #33f;">1</span> <span style="color: #33f;">1</span> <span style="color: #33f;">1</span> <a href="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/size.html"><span style="color: #0000FF;">size</span></a><span style="color: #080;">&#40;</span>dE,<span style="color: #33f;">4</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#93;</span><span style="color: #080;">&#41;</span>;<br />
&nbsp; &nbsp; filt_vol = filt_vol + <span style="color: #33f;">3</span>/<span style="color: #33f;">28</span> * <span style="color: #080;">&#40;</span><span style="color: #080;">&#40;</span><a href="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/exp.html"><span style="color: #0000FF;">exp</span></a><span style="color: #080;">&#40;</span>- <span style="color: #080;">&#40;</span>cE / kappa<span style="color: #080;">&#41;</span>.^<span style="color: #33f;">2</span> <span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span> .* <span style="color: #080;">&#40;</span>dE<span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>;<br />
&nbsp; &nbsp; <a href="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/clear.html"><span style="color: #0000FF;">clear</span></a> cE;<br />
&nbsp; &nbsp; <a href="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/clear.html"><span style="color: #0000FF;">clear</span></a> dE;<br />
<br />
&nbsp; &nbsp; dW = convn<span style="color: #080;">&#40;</span>filt_vol, <span style="color: #080;">&#91;</span>-<span style="color: #33f;">1</span> <span style="color: #33f;">1</span> <span style="color: #33f;">0</span><span style="color: #080;">&#93;</span>, <span style="color:#A020F0;">'full'</span><span style="color: #080;">&#41;</span>; dW=dW<span style="color: #080;">&#40;</span>:,<span style="color: #33f;">2</span>:ncols<span style="color: #080;">&#40;</span>dW<span style="color: #080;">&#41;</span>-<span style="color: #33f;">1</span>,:,:<span style="color: #080;">&#41;</span>; <br />
&nbsp; &nbsp; cW = <a href="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/repmat.html"><span style="color: #0000FF;">repmat</span></a><span style="color: #080;">&#40;</span><a href="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/sqrt.html"><span style="color: #0000FF;">sqrt</span></a><span style="color: #080;">&#40;</span><a href="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/sum.html"><span style="color: #0000FF;">sum</span></a><span style="color: #080;">&#40;</span>dW.^<span style="color: #33f;">2</span>, <span style="color: #33f;">4</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>, <span style="color: #080;">&#91;</span><span style="color: #33f;">1</span> <span style="color: #33f;">1</span> <span style="color: #33f;">1</span> <a href="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/size.html"><span style="color: #0000FF;">size</span></a><span style="color: #080;">&#40;</span>dW,<span style="color: #33f;">4</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#93;</span><span style="color: #080;">&#41;</span>;<br />
&nbsp; &nbsp; filt_vol = filt_vol - <span style="color: #33f;">3</span>/<span style="color: #33f;">28</span> * <span style="color: #080;">&#40;</span><span style="color: #080;">&#40;</span><a href="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/exp.html"><span style="color: #0000FF;">exp</span></a><span style="color: #080;">&#40;</span>- <span style="color: #080;">&#40;</span>cW / kappa<span style="color: #080;">&#41;</span>.^<span style="color: #33f;">2</span> <span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span> .* <span style="color: #080;">&#40;</span>dW<span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>;<br />
&nbsp; &nbsp; <a href="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/clear.html"><span style="color: #0000FF;">clear</span></a> dW;<br />
&nbsp; &nbsp; <a href="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/clear.html"><span style="color: #0000FF;">clear</span></a> cW;<br />
<br />
&nbsp; &nbsp; dN = convn<span style="color: #080;">&#40;</span>filt_vol, <span style="color: #080;">&#91;</span><span style="color: #33f;">0</span>; -<span style="color: #33f;">1</span>; <span style="color: #33f;">1</span><span style="color: #080;">&#93;</span>, <span style="color:#A020F0;">'full'</span><span style="color: #080;">&#41;</span>; dN=dN<span style="color: #080;">&#40;</span><span style="color: #33f;">2</span>:nrows<span style="color: #080;">&#40;</span>dN<span style="color: #080;">&#41;</span>-<span style="color: #33f;">1</span>,:,:,:<span style="color: #080;">&#41;</span>;<br />
&nbsp; &nbsp; cN = <a href="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/repmat.html"><span style="color: #0000FF;">repmat</span></a><span style="color: #080;">&#40;</span><a href="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/sqrt.html"><span style="color: #0000FF;">sqrt</span></a><span style="color: #080;">&#40;</span><a href="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/sum.html"><span style="color: #0000FF;">sum</span></a><span style="color: #080;">&#40;</span>dN.^<span style="color: #33f;">2</span>, <span style="color: #33f;">4</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>, <span style="color: #080;">&#91;</span><span style="color: #33f;">1</span> <span style="color: #33f;">1</span> <span style="color: #33f;">1</span> <a href="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/size.html"><span style="color: #0000FF;">size</span></a><span style="color: #080;">&#40;</span>dN,<span style="color: #33f;">4</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#93;</span><span style="color: #080;">&#41;</span>;<br />
&nbsp; &nbsp; filt_vol = filt_vol + <span style="color: #33f;">3</span>/<span style="color: #33f;">28</span> * <span style="color: #080;">&#40;</span><span style="color: #080;">&#40;</span><a href="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/exp.html"><span style="color: #0000FF;">exp</span></a><span style="color: #080;">&#40;</span>- <span style="color: #080;">&#40;</span>cN / kappa<span style="color: #080;">&#41;</span>.^<span style="color: #33f;">2</span> <span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span> .* <span style="color: #080;">&#40;</span>dN<span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>;<br />
&nbsp; &nbsp; <a href="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/clear.html"><span style="color: #0000FF;">clear</span></a> dN;<br />
&nbsp; &nbsp; <a href="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/clear.html"><span style="color: #0000FF;">clear</span></a> cN;<br />
<br />
&nbsp; &nbsp; dS = convn<span style="color: #080;">&#40;</span>filt_vol, <span style="color: #080;">&#91;</span>-<span style="color: #33f;">1</span>; <span style="color: #33f;">1</span>; <span style="color: #33f;">0</span><span style="color: #080;">&#93;</span>, <span style="color:#A020F0;">'full'</span><span style="color: #080;">&#41;</span>; dS=dS<span style="color: #080;">&#40;</span><span style="color: #33f;">2</span>:nrows<span style="color: #080;">&#40;</span>dS<span style="color: #080;">&#41;</span>-<span style="color: #33f;">1</span>,:,:,:<span style="color: #080;">&#41;</span>;<br />
&nbsp; &nbsp; cS = <a href="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/repmat.html"><span style="color: #0000FF;">repmat</span></a><span style="color: #080;">&#40;</span><a href="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/sqrt.html"><span style="color: #0000FF;">sqrt</span></a><span style="color: #080;">&#40;</span><a href="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/sum.html"><span style="color: #0000FF;">sum</span></a><span style="color: #080;">&#40;</span>dS.^<span style="color: #33f;">2</span>, <span style="color: #33f;">4</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>, <span style="color: #080;">&#91;</span><span style="color: #33f;">1</span> <span style="color: #33f;">1</span> <span style="color: #33f;">1</span> <a href="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/size.html"><span style="color: #0000FF;">size</span></a><span style="color: #080;">&#40;</span>dS,<span style="color: #33f;">4</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#93;</span><span style="color: #080;">&#41;</span>;<br />
&nbsp; &nbsp; filt_vol = filt_vol - <span style="color: #33f;">3</span>/<span style="color: #33f;">28</span> * <span style="color: #080;">&#40;</span><span style="color: #080;">&#40;</span><a href="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/exp.html"><span style="color: #0000FF;">exp</span></a><span style="color: #080;">&#40;</span>- <span style="color: #080;">&#40;</span>cS / kappa<span style="color: #080;">&#41;</span>.^<span style="color: #33f;">2</span> <span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span> .* <span style="color: #080;">&#40;</span>dS<span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>;<br />
&nbsp; &nbsp; <a href="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/clear.html"><span style="color: #0000FF;">clear</span></a> cS;<br />
&nbsp; &nbsp; <a href="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/clear.html"><span style="color: #0000FF;">clear</span></a> dS;<br />
<br />
&nbsp; &nbsp; kernel = <a href="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/zeros.html"><span style="color: #0000FF;">zeros</span></a><span style="color: #080;">&#40;</span><span style="color: #33f;">1</span>,<span style="color: #33f;">1</span>,<span style="color: #33f;">3</span><span style="color: #080;">&#41;</span>; kernel<span style="color: #080;">&#40;</span><span style="color: #33f;">2</span><span style="color: #080;">&#41;</span> = -<span style="color: #33f;">1</span>; kernel<span style="color: #080;">&#40;</span><span style="color: #33f;">3</span><span style="color: #080;">&#41;</span> = <span style="color: #33f;">1</span>; &nbsp; &nbsp;<br />
&nbsp; &nbsp; dU = convn<span style="color: #080;">&#40;</span>filt_vol, kernel, <span style="color:#A020F0;">'full'</span><span style="color: #080;">&#41;</span>; dU=dU<span style="color: #080;">&#40;</span>:,:,<span style="color: #33f;">2</span>:<a href="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/size.html"><span style="color: #0000FF;">size</span></a><span style="color: #080;">&#40;</span>dU,<span style="color: #33f;">3</span><span style="color: #080;">&#41;</span>-<span style="color: #33f;">1</span>,:<span style="color: #080;">&#41;</span>;<br />
&nbsp; &nbsp; cU = <a href="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/repmat.html"><span style="color: #0000FF;">repmat</span></a><span style="color: #080;">&#40;</span><a href="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/sqrt.html"><span style="color: #0000FF;">sqrt</span></a><span style="color: #080;">&#40;</span><a href="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/sum.html"><span style="color: #0000FF;">sum</span></a><span style="color: #080;">&#40;</span>dU.^<span style="color: #33f;">2</span>, <span style="color: #33f;">4</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>, <span style="color: #080;">&#91;</span><span style="color: #33f;">1</span> <span style="color: #33f;">1</span> <span style="color: #33f;">1</span> <a href="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/size.html"><span style="color: #0000FF;">size</span></a><span style="color: #080;">&#40;</span>dS,<span style="color: #33f;">4</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#93;</span><span style="color: #080;">&#41;</span>;<br />
&nbsp; &nbsp; filt_vol = filt_vol + <span style="color: #33f;">1</span>/<span style="color: #33f;">28</span> * <span style="color: #080;">&#40;</span><span style="color: #080;">&#40;</span><a href="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/exp.html"><span style="color: #0000FF;">exp</span></a><span style="color: #080;">&#40;</span>- <span style="color: #080;">&#40;</span>cU / kappa<span style="color: #080;">&#41;</span>.^<span style="color: #33f;">2</span> <span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span> .* <span style="color: #080;">&#40;</span>dU<span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>;<br />
&nbsp; &nbsp; <a href="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/clear.html"><span style="color: #0000FF;">clear</span></a> dU;<br />
&nbsp; &nbsp; <a href="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/clear.html"><span style="color: #0000FF;">clear</span></a> cU;<br />
<br />
&nbsp; &nbsp; kernel = <a href="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/zeros.html"><span style="color: #0000FF;">zeros</span></a><span style="color: #080;">&#40;</span><span style="color: #33f;">1</span>,<span style="color: #33f;">1</span>,<span style="color: #33f;">3</span><span style="color: #080;">&#41;</span>; kernel<span style="color: #080;">&#40;</span><span style="color: #33f;">1</span><span style="color: #080;">&#41;</span> = -<span style="color: #33f;">1</span>; kernel<span style="color: #080;">&#40;</span><span style="color: #33f;">2</span><span style="color: #080;">&#41;</span> = <span style="color: #33f;">1</span>;<br />
&nbsp; &nbsp; dD = convn<span style="color: #080;">&#40;</span>filt_vol, kernel, <span style="color:#A020F0;">'full'</span><span style="color: #080;">&#41;</span>; dD=dD<span style="color: #080;">&#40;</span>:,:,<span style="color: #33f;">2</span>:<a href="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/size.html"><span style="color: #0000FF;">size</span></a><span style="color: #080;">&#40;</span>dD,<span style="color: #33f;">3</span><span style="color: #080;">&#41;</span>-<span style="color: #33f;">1</span>,:<span style="color: #080;">&#41;</span>;<br />
&nbsp; &nbsp; <a href="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/cd.html"><span style="color: #0000FF;">cD</span></a> = <a href="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/repmat.html"><span style="color: #0000FF;">repmat</span></a><span style="color: #080;">&#40;</span><a href="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/sqrt.html"><span style="color: #0000FF;">sqrt</span></a><span style="color: #080;">&#40;</span><a href="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/sum.html"><span style="color: #0000FF;">sum</span></a><span style="color: #080;">&#40;</span>dD.^<span style="color: #33f;">2</span>, <span style="color: #33f;">4</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>, <span style="color: #080;">&#91;</span><span style="color: #33f;">1</span> <span style="color: #33f;">1</span> <span style="color: #33f;">1</span> <a href="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/size.html"><span style="color: #0000FF;">size</span></a><span style="color: #080;">&#40;</span>dS,<span style="color: #33f;">4</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#93;</span><span style="color: #080;">&#41;</span>;<br />
&nbsp; &nbsp; filt_vol = filt_vol - <span style="color: #33f;">1</span>/<span style="color: #33f;">28</span> * <span style="color: #080;">&#40;</span><span style="color: #080;">&#40;</span><a href="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/exp.html"><span style="color: #0000FF;">exp</span></a><span style="color: #080;">&#40;</span>- <span style="color: #080;">&#40;</span><a href="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/cd.html"><span style="color: #0000FF;">cD</span></a> / kappa<span style="color: #080;">&#41;</span>.^<span style="color: #33f;">2</span> <span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span> .* <span style="color: #080;">&#40;</span>dD<span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>;<br />
&nbsp; &nbsp; <a href="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/clear.html"><span style="color: #0000FF;">clear</span></a> dD;<br />
&nbsp; &nbsp; <a href="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/clear.html"><span style="color: #0000FF;">clear</span></a> <a href="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/cd.html"><span style="color: #0000FF;">cD</span></a>;<br />
<span style="color: #0000FF;">end</span></div></td></tr></tbody></table></div>
<h3>Python</h3>
<p>The Python code is very similar to the Matlab code above.  It does 2D images or 3D volumes, but I have not coded the smoothing across the 4th dimension.  That will have to be done later.</p>
<div class="codecolorer-container python geshi" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:99%;height:300px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br />19<br />20<br />21<br />22<br />23<br />24<br />25<br /></div></td><td><div class="python codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #ff7700;font-weight:bold;">def</span> aniso<span style="color: black;">&#40;</span>v, kappa=-<span style="color: #ff4500;">1</span>, N=<span style="color: #ff4500;">1</span><span style="color: black;">&#41;</span>:<br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">if</span> kappa == -<span style="color: #ff4500;">1</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; kappa = prctile<span style="color: black;">&#40;</span>v, <span style="color: #ff4500;">40</span><span style="color: black;">&#41;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; vf = v.<span style="color: #dc143c;">copy</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">for</span> ii <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #008000;">range</span><span style="color: black;">&#40;</span>N<span style="color: black;">&#41;</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; dE = -vf + roll<span style="color: black;">&#40;</span>vf,-<span style="color: #ff4500;">1</span>,<span style="color: #ff4500;">0</span><span style="color: black;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; dW = vf - roll<span style="color: black;">&#40;</span>vf,<span style="color: #ff4500;">1</span>,<span style="color: #ff4500;">0</span><span style="color: black;">&#41;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; dN = -vf + roll<span style="color: black;">&#40;</span>vf,-<span style="color: #ff4500;">1</span>,<span style="color: #ff4500;">1</span><span style="color: black;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; dS = vf - roll<span style="color: black;">&#40;</span>vf,<span style="color: #ff4500;">1</span>,<span style="color: #ff4500;">1</span><span style="color: black;">&#41;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #008000;">len</span><span style="color: black;">&#40;</span>v.<span style="color: black;">shape</span><span style="color: black;">&#41;</span> <span style="color: #66cc66;">&gt;</span> <span style="color: #ff4500;">2</span>: <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; dU = -vf + roll<span style="color: black;">&#40;</span>vf,-<span style="color: #ff4500;">1</span>,<span style="color: #ff4500;">2</span><span style="color: black;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; dD = vf - roll<span style="color: black;">&#40;</span>vf,<span style="color: #ff4500;">1</span>,<span style="color: #ff4500;">2</span><span style="color: black;">&#41;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; vf = vf + \<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ff4500;">3</span>./<span style="color: #ff4500;">28</span>. <span style="color: #66cc66;">*</span> <span style="color: black;">&#40;</span><span style="color: black;">&#40;</span>exp<span style="color: black;">&#40;</span>- <span style="color: black;">&#40;</span><span style="color: #008000;">abs</span><span style="color: black;">&#40;</span>dE<span style="color: black;">&#41;</span> / kappa<span style="color: black;">&#41;</span><span style="color: #66cc66;">**</span><span style="color: #ff4500;">2</span> <span style="color: black;">&#41;</span> <span style="color: #66cc66;">*</span> dE<span style="color: black;">&#41;</span> - <span style="color: black;">&#40;</span>exp<span style="color: black;">&#40;</span>- <span style="color: black;">&#40;</span><span style="color: #008000;">abs</span><span style="color: black;">&#40;</span>dW<span style="color: black;">&#41;</span> / kappa<span style="color: black;">&#41;</span><span style="color: #66cc66;">**</span><span style="color: #ff4500;">2</span> <span style="color: black;">&#41;</span> <span style="color: #66cc66;">*</span> dW<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span> + \<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ff4500;">3</span>./<span style="color: #ff4500;">28</span>. <span style="color: #66cc66;">*</span> <span style="color: black;">&#40;</span><span style="color: black;">&#40;</span>exp<span style="color: black;">&#40;</span>- <span style="color: black;">&#40;</span><span style="color: #008000;">abs</span><span style="color: black;">&#40;</span>dN<span style="color: black;">&#41;</span> / kappa<span style="color: black;">&#41;</span><span style="color: #66cc66;">**</span><span style="color: #ff4500;">2</span> <span style="color: black;">&#41;</span> <span style="color: #66cc66;">*</span> dN<span style="color: black;">&#41;</span> - <span style="color: black;">&#40;</span>exp<span style="color: black;">&#40;</span>- <span style="color: black;">&#40;</span><span style="color: #008000;">abs</span><span style="color: black;">&#40;</span>dS<span style="color: black;">&#41;</span> / kappa<span style="color: black;">&#41;</span><span style="color: #66cc66;">**</span><span style="color: #ff4500;">2</span> <span style="color: black;">&#41;</span> <span style="color: #66cc66;">*</span> dS<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #008000;">len</span><span style="color: black;">&#40;</span>v.<span style="color: black;">shape</span><span style="color: black;">&#41;</span> <span style="color: #66cc66;">&gt;</span> <span style="color: #ff4500;">2</span>: <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; vf += <span style="color: #ff4500;">1</span>./<span style="color: #ff4500;">28</span>. <span style="color: #66cc66;">*</span> <span style="color: black;">&#40;</span><span style="color: black;">&#40;</span>exp<span style="color: black;">&#40;</span>- <span style="color: black;">&#40;</span><span style="color: #008000;">abs</span><span style="color: black;">&#40;</span>dU<span style="color: black;">&#41;</span> / kappa<span style="color: black;">&#41;</span><span style="color: #66cc66;">**</span><span style="color: #ff4500;">2</span> <span style="color: black;">&#41;</span> <span style="color: #66cc66;">*</span> dU<span style="color: black;">&#41;</span> - <span style="color: black;">&#40;</span>exp<span style="color: black;">&#40;</span>- <span style="color: black;">&#40;</span><span style="color: #008000;">abs</span><span style="color: black;">&#40;</span>dD<span style="color: black;">&#41;</span> / kappa<span style="color: black;">&#41;</span><span style="color: #66cc66;">**</span><span style="color: #ff4500;">2</span> <span style="color: black;">&#41;</span> <span style="color: #66cc66;">*</span> dD<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span> <br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">return</span> vf</div></td></tr></tbody></table></div>
<p><span class="yafootnote_head"><br />
<h3>References</h3>
<p></span><br /><span class="yafootnote_body"><a name="foot_0">1.</a>&nbsp;G. Gerig et al., “Nonlinear anisotropic filtering of MRI data,” <span style="font-style: italic;">Medical Imaging, IEEE Transactions on</span> 11, no. 2 (1992): 221-232.<span class="Z3988" title="url_ver=Z39.88-2004&amp;ctx_ver=Z39.88-2004&amp;rft_val_fmt=info%3Aofi%2Ffmt%3Akev%3Amtx%3Ajournal&amp;rft.genre=article&amp;rft.atitle=Nonlinear%20anisotropic%20filtering%20of%20MRI%20data&amp;rft.jtitle=Medical%20Imaging%2C%20IEEE%20Transactions%20on&amp;rft.stitle=Medical%20Imaging%2C%20IEEE%20Transactions%20on&amp;rft.volume=11&amp;rft.issue=2&amp;rft.aufirst=G.&amp;rft.aulast=Gerig&amp;rft.au=G.%20Gerig&amp;rft.au=O.%20Kubler&amp;rft.au=R.%20Kikinis&amp;rft.au=F.A.%20Jolesz&amp;rft.date=1992&amp;rft.pages=221-232&amp;rft.issn=0278-0062"> </span> (<a href="#foot_src_0">&uarr;</a>)</span><br /><span class="yafootnote_body"><a name="foot_1">2.</a>&nbsp;Craig K Jones, Kenneth P Whittall, and Alex L MacKay, “Robust myelin water quantification: averaging vs. spatial filtering,” Magnetic Resonance in Medicine: Official Journal of the Society of Magnetic Resonance in Medicine / Society of Magnetic Resonance in Medicine 50, no. 1 (July 2003): 206-209 (<a href="#foot_src_1">&uarr;</a>)</span></p>
]]></content:encoded>
			<wfw:commentRss>http://mri.brechmos.org/2010/01/19/anisotropic-diffusion-image-filtering-in-mri/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Pulse Sequence Diagrammer</title>
		<link>http://mri.brechmos.org/2010/01/18/pulse-sequence-diagrammer/</link>
		<comments>http://mri.brechmos.org/2010/01/18/pulse-sequence-diagrammer/#comments</comments>
		<pubDate>Mon, 18 Jan 2010 13:59:53 +0000</pubDate>
		<dc:creator>craig</dc:creator>
				<category><![CDATA[MRI]]></category>
		<category><![CDATA[Matlab]]></category>
		<category><![CDATA[Code]]></category>

		<guid isPermaLink="false">http://mri.brechmos.org/?p=500</guid>
		<description><![CDATA[	
	<span class="Z3988" title="ctx_ver=Z39.88-2004&amp;rft_val_fmt=info%3Aofi%2Ffmt%3Akev%3Amtx%3Adc&amp;rfr_id=info%3Asid%2Focoins.info%3Agenerator&amp;rft.title=Pulse+Sequence+Diagrammer&amp;rft.aulast=Jones&amp;rft.aufirst=Craig&amp;rft.subject=MRI&amp;rft.subject=Matlab&amp;rft.source=Research%3A+Math%2C+Computing+and+MRI&amp;rft.date=2010-01-18&amp;rft.type=blogPost&amp;rft.format=text&amp;rft.identifier=http://mri.brechmos.org/2010/01/18/pulse-sequence-diagrammer/&amp;rft.language=English"></span>
Overview
The matlab code in this directory should facilitate creating publication quality PSDs (pulse sequence diagrams) using Matlab. Look at the example files (cse.m, cpmg.m and fse.m) to see how to use the code. All files are script files so this should run on any machine that Matlab runs on.

Code: mrpsd_12.tar.gz
Matlab Version …
I know that it [...]]]></description>
			<content:encoded><![CDATA[	
	<span class="Z3988" title="ctx_ver=Z39.88-2004&amp;rft_val_fmt=info%3Aofi%2Ffmt%3Akev%3Amtx%3Adc&amp;rfr_id=info%3Asid%2Focoins.info%3Agenerator&amp;rft.title=Pulse+Sequence+Diagrammer&amp;rft.aulast=Jones&amp;rft.aufirst=Craig&amp;rft.subject=MRI&amp;rft.subject=Matlab&amp;rft.source=Research%3A+Math%2C+Computing+and+MRI&amp;rft.date=2010-01-18&amp;rft.type=blogPost&amp;rft.format=text&amp;rft.identifier=http://mri.brechmos.org/2010/01/18/pulse-sequence-diagrammer/&amp;rft.language=English"></span>
<h2>Overview</h2>
<p>The matlab code in this directory should facilitate creating publication quality PSDs (pulse sequence diagrams) using Matlab. Look at the example files (cse.m, cpmg.m and fse.m) to see how to use the code. All files are script files so this should run on any machine that Matlab runs on.</p>
<p style="text-align: center;"><a rel="attachment wp-att-497" href="http://mri.brechmos.org/?attachment_id=497"><img class="aligncenter" title="fse" src="http://mri.brechmos.org/wp-content/uploads/2010/01/fse-450x389.jpg" alt="" width="450" height="389" /></a></p>
<p>Code: <a rel="attachment wp-att-501" href="http://mri.brechmos.org/2010/01/18/pulse-sequence-diagrammer/mrpsd_12tar/">mrpsd_12.tar.gz</a></p>
<h2>Matlab Version …</h2>
<p>I know that it worked under version 5.x of Matlab, but it should work under any newer version as well.</p>
<h2>Why under Matlab?</h2>
<p>Ahh.. good question.  There are many reasons:</p>
<p>1) Many people use Matlab for their data analysis and general coding.</p>
<p>2) All of the print facilities are built in (so you can print to JPEG, Postscript, BMP, TIFF etc etc).</p>
<p>3) Many things come free with the way that it is designed, for example, if you want to look at only one temporal section of your PSD, all you have to do is plot it up and then do: set(gca, ‘xlim’, [50 100]) (if you want to look at between 50ms and 100ms). USE YOUR IMAGINATION HERE. There are potentially lots of little things like the previous example that I have not even thought of.</p>
<h2>E-mail me</h2>
<p>I would be very interested in any suggestions, fixes (!) that you can send along to make this toolbox better. I would also like any more example files that plot up other pulse sequences (spectroscopy, EPI etc etc). My e-mail is craig@mri.jhu.edu. It is free software, I will not restrict use in any way, shape or form (other than don&#8217;t sell it). I would appreciate, though, any enhancements that you can. I will try to make available updates as often as possible.</p>
<h2>Standard Disclaimer</h2>
<p>By using the software, I accept absolute no responsibility for anything. Use it at your own risk. It is absolutely <a href="http://www.gnu.org/licenses/gpl-3.0.txt">GPL</a>&#8216;ed software.</p>
<p>Have fun with it.</p>
]]></content:encoded>
			<wfw:commentRss>http://mri.brechmos.org/2010/01/18/pulse-sequence-diagrammer/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Noise in MRI (magnitude) data</title>
		<link>http://mri.brechmos.org/2010/01/14/noise-in-mri-magnitude-data/</link>
		<comments>http://mri.brechmos.org/2010/01/14/noise-in-mri-magnitude-data/#comments</comments>
		<pubDate>Fri, 15 Jan 2010 04:53:00 +0000</pubDate>
		<dc:creator>craig</dc:creator>
				<category><![CDATA[MRI]]></category>
		<category><![CDATA[Matlab]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[Code]]></category>

		<guid isPermaLink="false">http://mri.brechmos.org/?p=355</guid>
		<description><![CDATA[	
	<span class="Z3988" title="ctx_ver=Z39.88-2004&amp;rft_val_fmt=info%3Aofi%2Ffmt%3Akev%3Amtx%3Adc&amp;rfr_id=info%3Asid%2Focoins.info%3Agenerator&amp;rft.title=Noise+in+MRI+%28magnitude%29+data&amp;rft.aulast=Jones&amp;rft.aufirst=Craig&amp;rft.subject=MRI&amp;rft.subject=Matlab&amp;rft.subject=Python&amp;rft.source=Research%3A+Math%2C+Computing+and+MRI&amp;rft.date=2010-01-14&amp;rft.type=blogPost&amp;rft.format=text&amp;rft.identifier=http://mri.brechmos.org/2010/01/14/noise-in-mri-magnitude-data/&amp;rft.language=English"></span>
Background
Magnitude MRI data has Rician noise distribution by definition [1].  It comes about because two channels each with Gaussian noise are squared and added together [2].  There is a longer description here.
Modeling
The Rician noise is created as , where  is the true signal, and  and  are random numbers from a Gaussian [...]]]></description>
			<content:encoded><![CDATA[	
	<span class="Z3988" title="ctx_ver=Z39.88-2004&amp;rft_val_fmt=info%3Aofi%2Ffmt%3Akev%3Amtx%3Adc&amp;rfr_id=info%3Asid%2Focoins.info%3Agenerator&amp;rft.title=Noise+in+MRI+%28magnitude%29+data&amp;rft.aulast=Jones&amp;rft.aufirst=Craig&amp;rft.subject=MRI&amp;rft.subject=Matlab&amp;rft.subject=Python&amp;rft.source=Research%3A+Math%2C+Computing+and+MRI&amp;rft.date=2010-01-14&amp;rft.type=blogPost&amp;rft.format=text&amp;rft.identifier=http://mri.brechmos.org/2010/01/14/noise-in-mri-magnitude-data/&amp;rft.language=English"></span>
<h2>Background</h2>
<p>Magnitude MRI data has<a href="http://en.wikipedia.org/wiki/Rice_distribution"> Rician noise distribution</a> by definition <a href="#foot_0" name="foot_src_0">[1]</a>.  It comes about because two channels each with Gaussian noise are squared and added together <a href="#foot_1" name="foot_src_1">[2]</a>.  There is a <a href="/research/magnetic-resonance-imaging/noise-in-mri-magnitude-data">longer description here</a>.</p>
<h2>Modeling</h2>
<p>The Rician noise is created as <img src='http://s.wordpress.com/latex.php?latex=y_e%28t_i%29%20%3D%20%5Csqrt%7B%20%5Cleft%5By%28t_i%29%20%2B%20e_1%20%5Cright%5D%5E2%20%2B%20e_2%5E2%20%7D&#038;bg=ffffff&#038;fg=000000&#038;s=0' alt='y_e(t_i) = \sqrt{ \left[y(t_i) + e_1 \right]^2 + e_2^2 }' title='y_e(t_i) = \sqrt{ \left[y(t_i) + e_1 \right]^2 + e_2^2 }' class='latex' />, where <img src='http://s.wordpress.com/latex.php?latex=y&#038;bg=ffffff&#038;fg=000000&#038;s=0' alt='y' title='y' class='latex' /> is the true signal, and <img src='http://s.wordpress.com/latex.php?latex=e_1&#038;bg=ffffff&#038;fg=000000&#038;s=0' alt='e_1' title='e_1' class='latex' /> and <img src='http://s.wordpress.com/latex.php?latex=e_2&#038;bg=ffffff&#038;fg=000000&#038;s=0' alt='e_2' title='e_2' class='latex' /> are random numbers from a Gaussian distribution with zero mean and standard deviation <img src='http://s.wordpress.com/latex.php?latex=%5Csigma&#038;bg=ffffff&#038;fg=000000&#038;s=0' alt='\sigma' title='\sigma' class='latex' />.  The standard deviation, <img src='http://s.wordpress.com/latex.php?latex=%5Csigma&#038;bg=ffffff&#038;fg=000000&#038;s=0' alt='\sigma' title='\sigma' class='latex' />, for the Gaussian distribution is related to the signal to noise ratio and is typically on the order of 1% &#8211; 10% of the signal <img src='http://s.wordpress.com/latex.php?latex=y&#038;bg=ffffff&#038;fg=000000&#038;s=0' alt='y' title='y' class='latex' />.</p>
<h2>Code</h2>
<p>It is relatively easy to model this using Matlab or Python. For the code here I am modeling a T2 decay curve and then the noise.</p>
<h3>Matlab</h3>
<div class="codecolorer-container matlab geshi" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:99%;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br /></div></td><td><div class="matlab codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #228B22;">%  Setup the initial variables</span><br />
rho = <span style="color: #33f;">100</span>;<br />
t2 = <span style="color: #33f;">80</span>; <span style="color: #228B22;">% in ms</span><br />
te = <span style="color: #33f;">10</span>:<span style="color: #33f;">10</span>:<span style="color: #33f;">320</span>;  <span style="color: #228B22;">% in ms</span><br />
<br />
<span style="color: #228B22;">%  Create a T2 decay curve</span><br />
y = rho * <a href="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/exp.html"><span style="color: #0000FF;">exp</span></a><span style="color: #080;">&#40;</span>-te / t2 <span style="color: #080;">&#41;</span>;<br />
<br />
<span style="color: #228B22;">%  Define the noise to be 5% of the signal</span><br />
s = <span style="color: #33f;">5</span>;<br />
<br />
<span style="color: #228B22;">%  Create the two Gaussian random variable vectors</span><br />
e1 = s * <a href="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/randn.html"><span style="color: #0000FF;">randn</span></a><span style="color: #080;">&#40;</span><a href="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/size.html"><span style="color: #0000FF;">size</span></a><span style="color: #080;">&#40;</span>y<span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>;<br />
e2 = s * <a href="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/randn.html"><span style="color: #0000FF;">randn</span></a><span style="color: #080;">&#40;</span><a href="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/size.html"><span style="color: #0000FF;">size</span></a><span style="color: #080;">&#40;</span>y<span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>;<br />
<br />
<span style="color: #228B22;">%  Now create the new, noisy decay curve.</span><br />
y_e = <a href="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/sqrt.html"><span style="color: #0000FF;">sqrt</span></a><span style="color: #080;">&#40;</span> <span style="color: #080;">&#40;</span>y+e1<span style="color: #080;">&#41;</span>.^<span style="color: #33f;">2</span> + <span style="color: #080;">&#40;</span>e2<span style="color: #080;">&#41;</span>.^<span style="color: #33f;">2</span> <span style="color: #080;">&#41;</span>;</div></td></tr></tbody></table></div>
<h3>Python</h3>
<p>The Python version is quite similar.</p>
<div class="codecolorer-container python geshi" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:99%;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br />19<br /></div></td><td><div class="python codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #ff7700;font-weight:bold;">from</span> <span style="color: #dc143c;">__future__</span> <span style="color: #ff7700;font-weight:bold;">import</span> division<br />
<br />
<span style="color: #808080; font-style: italic;">#  Setup the initial variables</span><br />
rho = <span style="color: #ff4500;">100</span><br />
t2 = <span style="color: #ff4500;">80</span> <span style="color: #808080; font-style: italic;"># in ms</span><br />
te = r_<span style="color: black;">&#91;</span><span style="color: #ff4500;">10</span>:<span style="color: #ff4500;">330</span>:<span style="color: #ff4500;">10</span><span style="color: black;">&#93;</span> <span style="color: #808080; font-style: italic;"># in ms</span><br />
<br />
<span style="color: #808080; font-style: italic;">#  Create a T2 decay curve</span><br />
y = rho <span style="color: #66cc66;">*</span> exp<span style="color: black;">&#40;</span> -te / t2 <span style="color: black;">&#41;</span><br />
<br />
<span style="color: #808080; font-style: italic;">#  Define the noise to be 5% of the signal</span><br />
s = <span style="color: #ff4500;">5</span><span style="color: #66cc66;">;</span><br />
<br />
<span style="color: #808080; font-style: italic;">#  Create the two Gaussian random variable vectors</span><br />
e1 = normal<span style="color: black;">&#40;</span><span style="color: #ff4500;">0</span>, <span style="color: #ff4500;">5</span>, y.<span style="color: black;">shape</span><span style="color: black;">&#41;</span><br />
e2 = normal<span style="color: black;">&#40;</span><span style="color: #ff4500;">0</span>, <span style="color: #ff4500;">5</span>, y.<span style="color: black;">shape</span><span style="color: black;">&#41;</span><br />
<br />
<span style="color: #808080; font-style: italic;">#  Now create the new, noisy decay curve.</span><br />
y_e = sqrt<span style="color: black;">&#40;</span> <span style="color: black;">&#40;</span>y+e1<span style="color: black;">&#41;</span><span style="color: #66cc66;">**</span><span style="color: #ff4500;">2</span> + <span style="color: black;">&#40;</span>e2<span style="color: black;">&#41;</span><span style="color: #66cc66;">**</span><span style="color: #ff4500;">2</span> <span style="color: black;">&#41;</span><span style="color: #66cc66;">;</span></div></td></tr></tbody></table></div>
<p>There are a couple of small gotcha&#8217;s that at least tripped me up as I am still relatively new to Python.</p>
<ol>
<li>The first is that <a href="http://www.python.org/dev/peps/pep-0238/">under Python 2.x all data is processed as integer</a> (not doubles, as the default is in Matlab).  Supposedly this is going to change in Python 3, but to get around it for now, the best thing to do is to add the <code class="codecolorer python geshi"><span class="python"><span style="color: #ff7700;font-weight:bold;">from</span> <span style="color: #dc143c;">__future__</span> <span style="color: #ff7700;font-weight:bold;">import</span> divison</span></code>.</li>
<li>To define <code class="codecolorer python geshi"><span class="python">te</span></code> I had to go to 330, rather than 320 as the generator is an open set on the higher end so it does not include the number.</li>
<li>There are several options for creating the random numbers.  There is a Python module called <code class="codecolorer python geshi"><span class="python"><span style="color: #dc143c;">random</span></span></code> that could be used.  Instead I used the Numpy <code class="codecolorer python geshi"><span class="python">normal</span></code> instead as I can pass in the shape parameter.</li>
</ol>
<p><span class="yafootnote_head"><br />
<h3>References</h3>
<p></span><br /><span class="yafootnote_body"><a name="foot_0">1.</a>&nbsp;Hákon Gudbjartsson and Samuel Patz, “The Rician Distribution of Noisy MRI Data,” Magnetic resonance in medicine : official journal of the Society of Magnetic Resonance in Medicine / Society of Magnetic Resonance in Medicine 34, no. 6 (December 1995): 910-914 (<a href="#foot_src_0">&uarr;</a>)</span><br /><span class="yafootnote_body"><a name="foot_1">2.</a>&nbsp;R M Henkelman, “Measurement of signal intensities in the presence of noise in MR images,” Medical Physics 12, no. 2 (April 1985): 232-233. (<a href="#foot_src_1">&uarr;</a>)</span></p>
]]></content:encoded>
			<wfw:commentRss>http://mri.brechmos.org/2010/01/14/noise-in-mri-magnitude-data/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Display Image with Black Background</title>
		<link>http://mri.brechmos.org/2009/07/23/display-image-with-black-background/</link>
		<comments>http://mri.brechmos.org/2009/07/23/display-image-with-black-background/#comments</comments>
		<pubDate>Fri, 24 Jul 2009 03:14:52 +0000</pubDate>
		<dc:creator>craig</dc:creator>
				<category><![CDATA[Matlab]]></category>
		<category><![CDATA[Code]]></category>

		<guid isPermaLink="false">http://mri.brechmos.org/?p=185</guid>
		<description><![CDATA[	
	<span class="Z3988" title="ctx_ver=Z39.88-2004&amp;rft_val_fmt=info%3Aofi%2Ffmt%3Akev%3Amtx%3Adc&amp;rfr_id=info%3Asid%2Focoins.info%3Agenerator&amp;rft.title=Display+Image+with+Black+Background&amp;rft.aulast=Jones&amp;rft.aufirst=Craig&amp;rft.subject=Matlab&amp;rft.source=Research%3A+Math%2C+Computing+and+MRI&amp;rft.date=2009-07-23&amp;rft.type=blogPost&amp;rft.format=text&amp;rft.identifier=http://mri.brechmos.org/2009/07/23/display-image-with-black-background/&amp;rft.language=English"></span>
The Problem
There are times when an image is created where we don’t do a calculation for part of the image and want to display it black (for example) and not the colormap color which would correspond to zero.
For example, in the image:

original let’s say we are only interested in the block in the center and [...]]]></description>
			<content:encoded><![CDATA[	
	<span class="Z3988" title="ctx_ver=Z39.88-2004&amp;rft_val_fmt=info%3Aofi%2Ffmt%3Akev%3Amtx%3Adc&amp;rfr_id=info%3Asid%2Focoins.info%3Agenerator&amp;rft.title=Display+Image+with+Black+Background&amp;rft.aulast=Jones&amp;rft.aufirst=Craig&amp;rft.subject=Matlab&amp;rft.source=Research%3A+Math%2C+Computing+and+MRI&amp;rft.date=2009-07-23&amp;rft.type=blogPost&amp;rft.format=text&amp;rft.identifier=http://mri.brechmos.org/2009/07/23/display-image-with-black-background/&amp;rft.language=English"></span>
<h3>The Problem</h3>
<p>There are times when an image is created where we don’t do a calculation for part of the image and want to display it black (for example) and not the colormap color which would correspond to zero.</p>
<p>For example, in the image:</p>
<p style="text-align: center;"><a href="http://mri.brechmos.org/wp-content/uploads/2009/07/23/display-image-with-black-background/original.jpg"><img class="aligncenter size-medium wp-image-187" title="original" src="http://mri.brechmos.org/wp-content/uploads/2009/07/23/display-image-with-black-background/original-300x225.jpg" alt="" width="300" height="225" /></a></p>
<p>original let’s say we are only interested in the block in the center and not the blue background around it.</p>
<p>We can create a background of NaN’s to represent the region which we want to not view, then use the alpha (transparency) channel to enable viewing only of the non-NaN region.</p>
<p><a href="http://mri.brechmos.org/wp-content/uploads/2009/07/23/display-image-with-black-background/blocked.jpg"><img class="aligncenter size-medium wp-image-189" title="blocked" src="http://mri.brechmos.org/wp-content/uploads/2009/07/23/display-image-with-black-background/blocked-300x225.jpg" alt="" width="300" height="225" /></a></p>
<h3>Code</h3>
<p>Given a set of data, for example:</p>
<div class="codecolorer-container matlab geshi" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:99%;height:300px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br />19<br />20<br />21<br />22<br />23<br />24<br />25<br />26<br />27<br />28<br />29<br /></div></td><td><div class="matlab codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #228B22;">%% Create the test image</span><br />
A = <a href="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/zeros.html"><span style="color: #0000FF;">zeros</span></a><span style="color: #080;">&#40;</span><span style="color: #33f;">128</span>,<span style="color: #33f;">128</span><span style="color: #080;">&#41;</span>;<br />
A<span style="color: #080;">&#40;</span><span style="color: #33f;">33</span>:<span style="color: #33f;">96</span>,<span style="color: #33f;">33</span>:<span style="color: #33f;">96</span><span style="color: #080;">&#41;</span> = <a href="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/repmat.html"><span style="color: #0000FF;">repmat</span></a><span style="color: #080;">&#40;</span> <a href="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/linspace.html"><span style="color: #0000FF;">linspace</span></a><span style="color: #080;">&#40;</span><span style="color: #33f;">0</span>, <span style="color: #33f;">1</span>, <span style="color: #33f;">64</span><span style="color: #080;">&#41;</span>, <span style="color: #080;">&#91;</span><span style="color: #33f;">64</span> <span style="color: #33f;">1</span><span style="color: #080;">&#93;</span><span style="color: #080;">&#41;</span>;<br />
<br />
<a href="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/figure.html"><span style="color: #0000FF;">figure</span></a><span style="color: #080;">&#40;</span><span style="color: #33f;">1</span><span style="color: #080;">&#41;</span>,<a href="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/clf.html"><span style="color: #0000FF;">clf</span></a>;<br />
imagesc<span style="color: #080;">&#40;</span> A <span style="color: #080;">&#41;</span>; <a href="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/axis.html"><span style="color: #0000FF;">axis</span></a><span style="color: #080;">&#40;</span><span style="color:#A020F0;">'off'</span><span style="color: #080;">&#41;</span>; <a href="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/axis.html"><span style="color: #0000FF;">axis</span></a><span style="color: #080;">&#40;</span><span style="color:#A020F0;">'square'</span><span style="color: #080;">&#41;</span>;<br />
<a href="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/colormap.html"><span style="color: #0000FF;">colormap</span></a><span style="color: #080;">&#40;</span><a href="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/jet.html"><span style="color: #0000FF;">jet</span></a><span style="color: #080;">&#41;</span>;<br />
<a href="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/colorbar.html"><span style="color: #0000FF;">colorbar</span></a>;<br />
<br />
<a href="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/print.html"><span style="color: #0000FF;">print</span></a> -f1 -djpeg original.<span style="">jpg</span><br />
<br />
<span style="color: #228B22;">%% The better image</span><br />
B = <a href="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/nan.html"><span style="color: #0000FF;">nan</span></a>*<a href="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/ones.html"><span style="color: #0000FF;">ones</span></a><span style="color: #080;">&#40;</span><span style="color: #33f;">128</span>,<span style="color: #33f;">128</span><span style="color: #080;">&#41;</span>;<br />
B<span style="color: #080;">&#40;</span><span style="color: #33f;">33</span>:<span style="color: #33f;">96</span>,<span style="color: #33f;">33</span>:<span style="color: #33f;">96</span><span style="color: #080;">&#41;</span> = <a href="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/repmat.html"><span style="color: #0000FF;">repmat</span></a><span style="color: #080;">&#40;</span> <a href="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/linspace.html"><span style="color: #0000FF;">linspace</span></a><span style="color: #080;">&#40;</span><span style="color: #33f;">0</span>, <span style="color: #33f;">1</span>, <span style="color: #33f;">64</span><span style="color: #080;">&#41;</span>, <span style="color: #080;">&#91;</span><span style="color: #33f;">64</span> <span style="color: #33f;">1</span><span style="color: #080;">&#93;</span><span style="color: #080;">&#41;</span>;<br />
<br />
<a href="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/figure.html"><span style="color: #0000FF;">figure</span></a><span style="color: #080;">&#40;</span><span style="color: #33f;">2</span><span style="color: #080;">&#41;</span>,<a href="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/clf.html"><span style="color: #0000FF;">clf</span></a>;<br />
imagesc<span style="color: #080;">&#40;</span> B <span style="color: #080;">&#41;</span>; <a href="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/axis.html"><span style="color: #0000FF;">axis</span></a><span style="color: #080;">&#40;</span><span style="color:#A020F0;">'off'</span><span style="color: #080;">&#41;</span>; <a href="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/axis.html"><span style="color: #0000FF;">axis</span></a><span style="color: #080;">&#40;</span><span style="color:#A020F0;">'square'</span><span style="color: #080;">&#41;</span>;<br />
<a href="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/colormap.html"><span style="color: #0000FF;">colormap</span></a><span style="color: #080;">&#40;</span><a href="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/jet.html"><span style="color: #0000FF;">jet</span></a><span style="color: #080;">&#41;</span>;<br />
<a href="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/colorbar.html"><span style="color: #0000FF;">colorbar</span></a>;<br />
<br />
<span style="color: #228B22;">%% &nbsp;Now set the alpha map for the nan region</span><br />
z = B;<br />
z<span style="color: #080;">&#40;</span>~isnan<span style="color: #080;">&#40;</span>B<span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span> = <span style="color: #33f;">1</span>;<br />
z<span style="color: #080;">&#40;</span>isnan<span style="color: #080;">&#40;</span>B<span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span> = <span style="color: #33f;">0</span>;<br />
<br />
alpha<span style="color: #080;">&#40;</span>z<span style="color: #080;">&#41;</span>;<br />
<a href="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/set.html"><span style="color: #0000FF;">set</span></a><span style="color: #080;">&#40;</span><a href="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/gca.html"><span style="color: #0000FF;">gca</span></a>, <span style="color:#A020F0;">'color'</span>, <span style="color: #080;">&#91;</span><span style="color: #33f;">0</span> <span style="color: #33f;">0</span> <span style="color: #33f;">0</span><span style="color: #080;">&#93;</span><span style="color: #080;">&#41;</span>;<br />
<br />
<a href="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/print.html"><span style="color: #0000FF;">print</span></a> -f2 -djpeg blocked.<span style="">jpg</span></div></td></tr></tbody></table></div>
]]></content:encoded>
			<wfw:commentRss>http://mri.brechmos.org/2009/07/23/display-image-with-black-background/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Propagation of Errors for MTR Asymmetry</title>
		<link>http://mri.brechmos.org/2009/07/13/propagation-of-errors-for-mtr-asymmetry/</link>
		<comments>http://mri.brechmos.org/2009/07/13/propagation-of-errors-for-mtr-asymmetry/#comments</comments>
		<pubDate>Tue, 14 Jul 2009 03:21:20 +0000</pubDate>
		<dc:creator>craig</dc:creator>
				<category><![CDATA[Matlab]]></category>
		<category><![CDATA[Code]]></category>
		<category><![CDATA[MRI]]></category>

		<guid isPermaLink="false">http://mri.brechmos.org/?p=191</guid>
		<description><![CDATA[	
	<span class="Z3988" title="ctx_ver=Z39.88-2004&amp;rft_val_fmt=info%3Aofi%2Ffmt%3Akev%3Amtx%3Adc&amp;rfr_id=info%3Asid%2Focoins.info%3Agenerator&amp;rft.title=Propagation+of+Errors+for+MTR+Asymmetry&amp;rft.aulast=Jones&amp;rft.aufirst=Craig&amp;rft.subject=Matlab&amp;rft.source=Research%3A+Math%2C+Computing+and+MRI&amp;rft.date=2009-07-13&amp;rft.type=blogPost&amp;rft.format=text&amp;rft.identifier=http://mri.brechmos.org/2009/07/13/propagation-of-errors-for-mtr-asymmetry/&amp;rft.language=English"></span>
The Problem
It is important to calculate the propagation of errors for combinations of variables. There are some good resources on the interenet for linear propagation of error including Wikipedia (here).
It is assumed the MTR z-spectral data has means and standard devations for positive offsets (mmp, ssp) and means and standard deviations for negative offsets (mmn, [...]]]></description>
			<content:encoded><![CDATA[	
	<span class="Z3988" title="ctx_ver=Z39.88-2004&amp;rft_val_fmt=info%3Aofi%2Ffmt%3Akev%3Amtx%3Adc&amp;rfr_id=info%3Asid%2Focoins.info%3Agenerator&amp;rft.title=Propagation+of+Errors+for+MTR+Asymmetry&amp;rft.aulast=Jones&amp;rft.aufirst=Craig&amp;rft.subject=Matlab&amp;rft.source=Research%3A+Math%2C+Computing+and+MRI&amp;rft.date=2009-07-13&amp;rft.type=blogPost&amp;rft.format=text&amp;rft.identifier=http://mri.brechmos.org/2009/07/13/propagation-of-errors-for-mtr-asymmetry/&amp;rft.language=English"></span>
<h3>The Problem</h3>
<p>It is important to calculate the propagation of errors for combinations of variables. There are some good resources on the interenet for linear propagation of error including Wikipedia (<a onclick="javascript:pageTracker._trackPageview('/outbound/article/en.wikipedia.org');" href="http://en.wikipedia.org/wiki/Propagation_of_uncertainty">here</a>).</p>
<p>It is assumed the MTR z-spectral data has means and standard devations for positive offsets (mmp, ssp) and means and standard deviations for negative offsets (mmn, ssn). The asymmetry is calculated as <code class="codecolorer matlab geshi"><span class="matlab">&nbsp;mm = <span style="color: #080;">&#40;</span>mmp - mmn<span style="color: #080;">&#41;</span> ./ mmp</span></code>.</p>
<h3>Code</h3>
<p>Given a set of data, for example:</p>
<div class="codecolorer-container matlab geshi" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:99%;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br /></div></td><td><div class="matlab codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #0000FF;">function</span> <span style="color: #080;">&#91;</span>mm,ss<span style="color: #080;">&#93;</span> = calcasym<span style="color: #080;">&#40;</span>mmp, ssp, mmn, ssn<span style="color: #080;">&#41;</span><br />
<br />
mmn = <a href="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/fliplr.html"><span style="color: #0000FF;">fliplr</span></a><span style="color: #080;">&#40;</span>mmn<span style="color: #080;">&#41;</span>;<br />
ssn = <a href="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/fliplr.html"><span style="color: #0000FF;">fliplr</span></a><span style="color: #080;">&#40;</span>ssn<span style="color: #080;">&#41;</span>;<br />
<br />
<span style="color: #228B22;">%% Calculate the asymmetry</span><br />
mm = <span style="color: #080;">&#40;</span>mmp - mmn<span style="color: #080;">&#41;</span> ./ mmp;<br />
<br />
<span style="color: #228B22;">%% Calculate the error.</span><br />
mean_num = mmp-mmn;<br />
std_num = <a href="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/sqrt.html"><span style="color: #0000FF;">sqrt</span></a><span style="color: #080;">&#40;</span> ssp.^<span style="color: #33f;">2</span> + ssn.^<span style="color: #33f;">2</span> <span style="color: #080;">&#41;</span>;<br />
<br />
mean_denom = mmp;<br />
std_denom = ssp;<br />
<br />
ss = <span style="color: #080;">&#40;</span>mean_num./mean_denom<span style="color: #080;">&#41;</span> .* <a href="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/sqrt.html"><span style="color: #0000FF;">sqrt</span></a><span style="color: #080;">&#40;</span> <span style="color: #080;">&#40;</span>std_num ./ mean_num<span style="color: #080;">&#41;</span>.^<span style="color: #33f;">2</span> + <span style="color: #080;">&#40;</span>std_denom ./ mean_denom<span style="color: #080;">&#41;</span>.^<span style="color: #33f;">2</span> <span style="color: #080;">&#41;</span>;<br />
<br />
mm = mm * <span style="color: #33f;">100</span>; ss = ss * <span style="color: #33f;">100</span>;</div></td></tr></tbody></table></div>
]]></content:encoded>
			<wfw:commentRss>http://mri.brechmos.org/2009/07/13/propagation-of-errors-for-mtr-asymmetry/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>nrows(d), ncols(d)</title>
		<link>http://mri.brechmos.org/2009/05/18/nrowsd-ncolsd/</link>
		<comments>http://mri.brechmos.org/2009/05/18/nrowsd-ncolsd/#comments</comments>
		<pubDate>Tue, 19 May 2009 00:32:17 +0000</pubDate>
		<dc:creator>craig</dc:creator>
				<category><![CDATA[Matlab]]></category>
		<category><![CDATA[Code]]></category>

		<guid isPermaLink="false">http://mri.brechmos.org/?p=304</guid>
		<description><![CDATA[	
	<span class="Z3988" title="ctx_ver=Z39.88-2004&amp;rft_val_fmt=info%3Aofi%2Ffmt%3Akev%3Amtx%3Adc&amp;rfr_id=info%3Asid%2Focoins.info%3Agenerator&amp;rft.title=nrows%28d%29%2C+ncols%28d%29&amp;rft.aulast=Jones&amp;rft.aufirst=Craig&amp;rft.subject=Matlab&amp;rft.source=Research%3A+Math%2C+Computing+and+MRI&amp;rft.date=2009-05-18&amp;rft.type=blogPost&amp;rft.format=text&amp;rft.identifier=http://mri.brechmos.org/2009/05/18/nrowsd-ncolsd/&amp;rft.language=English"></span>
I found myself continually typing size(A,1) and size(A,2) to get the number of rows and columns. Finally, I just wrote the (very) simple scripts nrows.m and ncols.m. All they do is call the size command but it makes code easier to read…
1&#62;&#62; B = zeros&#40; nrows&#40;A&#41;, ncols&#40;A&#41; &#41;;
The obvious and simple definitions are:
12function &#91;n&#93; = [...]]]></description>
			<content:encoded><![CDATA[	
	<span class="Z3988" title="ctx_ver=Z39.88-2004&amp;rft_val_fmt=info%3Aofi%2Ffmt%3Akev%3Amtx%3Adc&amp;rfr_id=info%3Asid%2Focoins.info%3Agenerator&amp;rft.title=nrows%28d%29%2C+ncols%28d%29&amp;rft.aulast=Jones&amp;rft.aufirst=Craig&amp;rft.subject=Matlab&amp;rft.source=Research%3A+Math%2C+Computing+and+MRI&amp;rft.date=2009-05-18&amp;rft.type=blogPost&amp;rft.format=text&amp;rft.identifier=http://mri.brechmos.org/2009/05/18/nrowsd-ncolsd/&amp;rft.language=English"></span>
<p>I found myself continually typing size(A,1) and size(A,2) to get the number of rows and columns. Finally, I just wrote the (very) simple scripts nrows.m and ncols.m. All they do is call the size command but it makes code easier to read…</p>
<div class="codecolorer-container matlab geshi" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:99%;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br /></div></td><td><div class="matlab codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">&gt;&gt; B = <a href="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/zeros.html"><span style="color: #0000FF;">zeros</span></a><span style="color: #080;">&#40;</span> nrows<span style="color: #080;">&#40;</span>A<span style="color: #080;">&#41;</span>, ncols<span style="color: #080;">&#40;</span>A<span style="color: #080;">&#41;</span> <span style="color: #080;">&#41;</span>;</div></td></tr></tbody></table></div>
<p>The obvious and simple definitions are:</p>
<div class="codecolorer-container matlab geshi" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:99%;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br /></div></td><td><div class="matlab codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #0000FF;">function</span> <span style="color: #080;">&#91;</span>n<span style="color: #080;">&#93;</span> = nrows<span style="color: #080;">&#40;</span>d<span style="color: #080;">&#41;</span><br />
n = <a href="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/size.html"><span style="color: #0000FF;">size</span></a><span style="color: #080;">&#40;</span>d,<span style="color: #33f;">1</span><span style="color: #080;">&#41;</span>;</div></td></tr></tbody></table></div>
<div class="codecolorer-container matlab geshi" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:99%;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br /></div></td><td><div class="matlab codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #0000FF;">function</span> <span style="color: #080;">&#91;</span>n<span style="color: #080;">&#93;</span> = ncols<span style="color: #080;">&#40;</span>d<span style="color: #080;">&#41;</span><br />
n = <a href="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/size.html"><span style="color: #0000FF;">size</span></a><span style="color: #080;">&#40;</span>d,<span style="color: #33f;">2</span><span style="color: #080;">&#41;</span>;</div></td></tr></tbody></table></div>
]]></content:encoded>
			<wfw:commentRss>http://mri.brechmos.org/2009/05/18/nrowsd-ncolsd/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Flatten an array of data</title>
		<link>http://mri.brechmos.org/2009/04/03/flatten-an-array-of-data/</link>
		<comments>http://mri.brechmos.org/2009/04/03/flatten-an-array-of-data/#comments</comments>
		<pubDate>Sat, 04 Apr 2009 00:30:57 +0000</pubDate>
		<dc:creator>craig</dc:creator>
				<category><![CDATA[Matlab]]></category>
		<category><![CDATA[Code]]></category>

		<guid isPermaLink="false">http://mri.brechmos.org/?p=301</guid>
		<description><![CDATA[	
	<span class="Z3988" title="ctx_ver=Z39.88-2004&amp;rft_val_fmt=info%3Aofi%2Ffmt%3Akev%3Amtx%3Adc&amp;rfr_id=info%3Asid%2Focoins.info%3Agenerator&amp;rft.title=Flatten+an+array+of+data&amp;rft.aulast=Jones&amp;rft.aufirst=Craig&amp;rft.subject=Matlab&amp;rft.source=Research%3A+Math%2C+Computing+and+MRI&amp;rft.date=2009-04-03&amp;rft.type=blogPost&amp;rft.format=text&amp;rft.identifier=http://mri.brechmos.org/2009/04/03/flatten-an-array-of-data/&amp;rft.language=English"></span>
This function is much more useful.  There are many times I have found myself wanting to find the maximum signal intensity of a set of images across the fourth dimension, for example the maximum intensity of A&#40;:,:,10,:&#41;.  You can’t do this simply as you can’t do something like A&#40;:,:,10,:&#41;&#40;:&#41;.  So, I wrote [...]]]></description>
			<content:encoded><![CDATA[	
	<span class="Z3988" title="ctx_ver=Z39.88-2004&amp;rft_val_fmt=info%3Aofi%2Ffmt%3Akev%3Amtx%3Adc&amp;rfr_id=info%3Asid%2Focoins.info%3Agenerator&amp;rft.title=Flatten+an+array+of+data&amp;rft.aulast=Jones&amp;rft.aufirst=Craig&amp;rft.subject=Matlab&amp;rft.source=Research%3A+Math%2C+Computing+and+MRI&amp;rft.date=2009-04-03&amp;rft.type=blogPost&amp;rft.format=text&amp;rft.identifier=http://mri.brechmos.org/2009/04/03/flatten-an-array-of-data/&amp;rft.language=English"></span>
<p>This function is much more useful.  There are many times I have found myself wanting to find the maximum signal intensity of a set of images across the fourth dimension, for example the maximum intensity of <code class="codecolorer matlab geshi"><span class="matlab">A<span style="color: #080;">&#40;</span>:,:,<span style="color: #33f;">10</span>,:<span style="color: #080;">&#41;</span></span></code>.  You can’t do this simply as you can’t do something like <code class="codecolorer matlab geshi"><span class="matlab">A<span style="color: #080;">&#40;</span>:,:,<span style="color: #33f;">10</span>,:<span style="color: #080;">&#41;</span><span style="color: #080;">&#40;</span>:<span style="color: #080;">&#41;</span></span></code>.  So, I wrote a simple function called <code class="codecolorer matlab geshi"><span class="matlab">flat.<span style="">m</span></span></code> to return a 1D version of the input matrix <code class="codecolorer matlab geshi"><span class="matlab">flat<span style="color: #080;">&#40;</span>A<span style="color: #080;">&#41;</span> = A<span style="color: #080;">&#40;</span>:<span style="color: #080;">&#41;</span>;</span></code>. So now you can call <code class="codecolorer matlab geshi"><span class="matlab">mm = <a href="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/max.html"><span style="color: #0000FF;">max</span></a><span style="color: #080;">&#40;</span> flat<span style="color: #080;">&#40;</span> A<span style="color: #080;">&#40;</span>:,:,<span style="color: #33f;">10</span>,:<span style="color: #080;">&#41;</span> <span style="color: #080;">&#41;</span> <span style="color: #080;">&#41;</span>;</span></code>.</p>
<div class="codecolorer-container matlab geshi" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:99%;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br /></div></td><td><div class="matlab codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #0000FF;">function</span> vec = flat<span style="color: #080;">&#40;</span>v<span style="color: #080;">&#41;</span><br />
<br />
vec = v<span style="color: #080;">&#40;</span>:<span style="color: #080;">&#41;</span>;</div></td></tr></tbody></table></div>
]]></content:encoded>
			<wfw:commentRss>http://mri.brechmos.org/2009/04/03/flatten-an-array-of-data/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
