<?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; MRI</title>
	<atom:link href="http://mri.brechmos.org/category/mri/feed/" rel="self" type="application/rss+xml" />
	<link>http://mri.brechmos.org</link>
	<description>by Craig Jones</description>
	<lastBuildDate>Wed, 10 Aug 2011 16:53:14 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>writeanalyze in Matlab</title>
		<link>http://mri.brechmos.org/2010/02/writeanalyze-in-matlab/</link>
		<comments>http://mri.brechmos.org/2010/02/writeanalyze-in-matlab/#comments</comments>
		<pubDate>Mon, 08 Feb 2010 14:57:23 +0000</pubDate>
		<dc:creator>craig</dc:creator>
				<category><![CDATA[Matlab]]></category>
		<category><![CDATA[MRI]]></category>
		<category><![CDATA[Code]]></category>

		<guid isPermaLink="false">http://mri.brechmos.org/?p=565</guid>
		<description><![CDATA[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 &#8230; <a href="http://mri.brechmos.org/2010/02/writeanalyze-in-matlab/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<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>
<p>[cc lang="matlab"]<br />
function [] = writeanalyze(fname, data, ftype)</p>
<p>if( nargin == 2 )<br />
	ftype = &#8216;int16&#8242;;<br />
end</p>
<p>if( strcmp( ftype, &#8216;int16&#8242; ) == 1 )<br />
	file_type = 4; bpp = 16;<br />
elseif( strcmp( ftype, &#8216;uint16&#8242; ) == 1 )<br />
	file_type = 4; bpp = 16;<br />
elseif( strcmp( ftype, &#8216;int32&#8242; ) == 1 )<br />
	file_type = 8; bpp = 32;<br />
elseif( strcmp( ftype, &#8216;float&#8217; ) == 1 )<br />
	file_type = 16; bpp = 32;<br />
elseif( strcmp( ftype, &#8216;double&#8217; ) == 1 )<br />
	file_type = 64; bpp = 64;<br />
else<br />
	error(sprintf(&#8216;Unknown data type %s&#8217;, ftype));<br />
end</p>
<p>fp = fopen([fname '.hdr'], &#8216;wb&#8217;, &#8216;b&#8217;);</p>
<p>%%<br />
%%  Write the header_key part<br />
%%<br />
fwrite(fp, 348, &#8216;int32&#8242;);<br />
fwrite(fp, repmat(&#8216; &#8216;, 1, 10), &#8216;char&#8217;);<br />
fwrite(fp, repmat(&#8216; &#8216;, 1, 18), &#8216;char&#8217;);<br />
fwrite(fp, 16384, &#8216;int32&#8242;);<br />
fwrite(fp, 0, &#8216;int16&#8242;);<br />
fwrite(fp, &#8216;r &#8216;, &#8216;char&#8217;);</p>
<p>%%<br />
%% Write the image_dimension part.<br />
%%<br />
fwrite(fp, length( size(data) ), &#8216;int16&#8242;);<br />
for ii=1:length( size(data) )<br />
	fwrite(fp, size(data,ii), &#8216;int16&#8242;);<br />
end</p>
<p>for ii=length( size(data) )+1:7<br />
	fwrite(fp, 1, &#8216;int16&#8242;);<br />
end	</p>
<p>fwrite(fp, 0, &#8216;int16&#8242;);  % unused 8<br />
fwrite(fp, 0, &#8216;int16&#8242;);  % unused 9<br />
fwrite(fp, 0, &#8216;int16&#8242;);  % unused 10<br />
fwrite(fp, 0, &#8216;int16&#8242;);  % unused 11<br />
fwrite(fp, 0, &#8216;int16&#8242;);  % unused 12<br />
fwrite(fp, 0, &#8216;int16&#8242;);  % unused 13<br />
fwrite(fp, 0, &#8216;int16&#8242;);  % unused 14</p>
<p>% data type<br />
fwrite(fp, file_type, &#8216;int16&#8242;);   % 4 = signed short<br />
fwrite(fp, bpp, &#8216;int16&#8242;);   % bpp<br />
fwrite(fp, 0, &#8216;int16&#8242;);<br />
for ii=1:8<br />
	fwrite(fp, 1.0, &#8216;float32&#8242;);<br />
end<br />
fwrite(fp, 0, &#8216;float32&#8242;);<br />
fwrite(fp, 0, &#8216;float32&#8242;); % funused 1<br />
fwrite(fp, 0, &#8216;float32&#8242;); % funused 2<br />
fwrite(fp, 0, &#8216;float32&#8242;); % funused 3</p>
<p>fwrite(fp, max(data(:)), &#8216;float32&#8242;);<br />
fwrite(fp, min(data(:)), &#8216;float32&#8242;);<br />
fwrite(fp, 0, &#8216;float32&#8242;);<br />
fwrite(fp, 0, &#8216;float32&#8242;); </p>
<p>fwrite(fp, round(max(data(:))), &#8216;int32&#8242;);   % glmax<br />
fwrite(fp, round(min(data(:))), &#8216;int32&#8242;);   % glmin</p>
<p>%%<br />
%%  Data history<br />
%%<br />
fwrite(fp, repmat(&#8216; &#8216;, 1, 80), &#8216;char&#8217;); % descrip<br />
fwrite(fp, repmat(&#8216; &#8216;, 1, 24), &#8216;char&#8217;); % aux_file<br />
fwrite(fp, &#8217;3&#8242;, &#8216;char&#8217;); % aux_file<br />
fwrite(fp, repmat(&#8216; &#8216;, 1, 10), &#8216;char&#8217;); % originator<br />
fwrite(fp, repmat(&#8216; &#8216;, 1, 10), &#8216;char&#8217;); % originator<br />
fwrite(fp, repmat(&#8216; &#8216;, 1, 10), &#8216;char&#8217;); % originator<br />
fwrite(fp, repmat(&#8216; &#8216;, 1, 10), &#8216;char&#8217;); % originator<br />
fwrite(fp, repmat(&#8216; &#8216;, 1, 10), &#8216;char&#8217;); % originator<br />
fwrite(fp, repmat(&#8216; &#8216;, 1, 10), &#8216;char&#8217;); % originator<br />
fwrite(fp, repmat(&#8216; &#8216;, 1, 3), &#8216;char&#8217;); % originator</p>
<p>fwrite(fp, 0, &#8216;int32&#8242;); % views<br />
fwrite(fp, 0, &#8216;int32&#8242;); % vols_added<br />
fwrite(fp, 0, &#8216;int32&#8242;); % start_fiedl<br />
fwrite(fp, 0, &#8216;int32&#8242;); % field_skip<br />
fwrite(fp, 0, &#8216;int32&#8242;); % omax<br />
fwrite(fp, 0, &#8216;int32&#8242;); % omin<br />
fwrite(fp, 0, &#8216;int32&#8242;); % small_max<br />
fwrite(fp, 0, &#8216;int32&#8242;); % small_min</p>
<p>fclose(fp);</p>
<p>%%<br />
%%  Write the data<br />
%%<br />
fp = fopen([fname '.img'], &#8216;wb&#8217;, &#8216;b&#8217;);<br />
fwrite(fp, data, ftype);<br />
fclose(fp);<br />
[/cc]</p>
]]></content:encoded>
			<wfw:commentRss>http://mri.brechmos.org/2010/02/writeanalyze-in-matlab/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Anisotropic Diffusion Image Filtering in MRI</title>
		<link>http://mri.brechmos.org/2010/01/anisotropic-diffusion-image-filtering-in-mri/</link>
		<comments>http://mri.brechmos.org/2010/01/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[Matlab]]></category>
		<category><![CDATA[MRI]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[Code]]></category>

		<guid isPermaLink="false">http://mri.brechmos.org/?p=432</guid>
		<description><![CDATA[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 &#8230; <a href="http://mri.brechmos.org/2010/01/anisotropic-diffusion-image-filtering-in-mri/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<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 ((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>)).  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 ((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)).</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:<br />
[cc lang="matlab"]</p>
<p>function [filt_vol] = aniso3d(orig_vol, kappa, niters)</p>
<p>if( nargin &lt; 3 )<br />
error(&#8216;aniso3d: Need more parameters&#8217;);<br />
end</p>
<p>filt_vol = orig_vol;</p>
<p>for iters = 1:niters</p>
<p>    dE = convn(filt_vol, [0 -1 1], &#8216;full&#8217;); dE=dE(:,2:ncols(dE)-1,:);<br />
    dW = convn(filt_vol, [-1 1 0], &#8216;full&#8217;); dW=dW(:,2:ncols(dW)-1,:);<br />
    dN = convn(filt_vol, [0; -1; 1], &#8216;full&#8217;); dN=dN(2:nrows(dN)-1,:,:);<br />
    dS = convn(filt_vol, [-1; 1; 0], &#8216;full&#8217;); dS=dS(2:nrows(dS)-1,:,:);<br />
    kernel = zeros(1,1,3); kernel(2) = -1; kernel(3) = 1;<br />
    dU = convn(filt_vol, kernel, &#8216;full&#8217;); dU=dU(:,:,2:size(dU,3)-1);<br />
    kernel = zeros(1,1,3); kernel(1) = -1; kernel(2) = 1;<br />
    dD = convn(filt_vol, kernel, &#8216;full&#8217;); dD=dD(:,:,2:size(dD,3)-1);</p>
<p>    filt_vol = filt_vol +  &#8230;<br />
        3/28 * ((double(exp(- (abs(dE) / kappa).^2 )) .* double(dE)) &#8211; (double(exp(- (abs(dW) / kappa).^2 )) .* double(dW))) + &#8230;<br />
        3/28 * ((double(exp(- (abs(dN) / kappa).^2 )) .* double(dN)) &#8211; (double(exp(- (abs(dS) / kappa).^2 )) .*  double(dS))) + &#8230;<br />
        1/28 * ((double(exp(- (abs(dU) / kappa).^2 )) .* double(dU)) &#8211; (double(exp(- (abs(dD) / kappa).^2 )) .* double(dD)));<br />
end<br />
[/cc]</p>
<p>For 4D data one can also smooth across the 4th dimension (whether it is time, diffusion etc).<br />
[cc lang="matlab"]<br />
function [filt_vol] = aniso3d_chan(orig_vol, kappa, niters)<br />
%<br />
%  aniso3d_chan &#8211; Run the anisotropic diffusion filter in 3D<br />
%                 and over the multiple channels.<br />
%</p>
<p>if( nargin &lt; 3 )<br />
error(&#8216;aniso3d: Need more parameters&#8217;);<br />
end</p>
<p>filt_vol = float(squeeze(orig_vol));</p>
<p>for iters = 1:niters<br />
    dE = convn(filt_vol, [0 -1 1], &#8216;full&#8217;); dE=dE(:,2:ncols(dE)-1,:,:);<br />
    cE = repmat(sqrt(sum(dE.^2, 4)), [1 1 1 size(dE,4)]);<br />
    filt_vol = filt_vol + 3/28 * ((exp(- (cE / kappa).^2 )) .* (dE));<br />
    clear cE;<br />
    clear dE;</p>
<p>    dW = convn(filt_vol, [-1 1 0], &#8216;full&#8217;); dW=dW(:,2:ncols(dW)-1,:,:);<br />
    cW = repmat(sqrt(sum(dW.^2, 4)), [1 1 1 size(dW,4)]);<br />
    filt_vol = filt_vol &#8211; 3/28 * ((exp(- (cW / kappa).^2 )) .* (dW));<br />
    clear dW;<br />
    clear cW;</p>
<p>    dN = convn(filt_vol, [0; -1; 1], &#8216;full&#8217;); dN=dN(2:nrows(dN)-1,:,:,:);<br />
    cN = repmat(sqrt(sum(dN.^2, 4)), [1 1 1 size(dN,4)]);<br />
    filt_vol = filt_vol + 3/28 * ((exp(- (cN / kappa).^2 )) .* (dN));<br />
    clear dN;<br />
    clear cN;</p>
<p>    dS = convn(filt_vol, [-1; 1; 0], &#8216;full&#8217;); dS=dS(2:nrows(dS)-1,:,:,:);<br />
    cS = repmat(sqrt(sum(dS.^2, 4)), [1 1 1 size(dS,4)]);<br />
    filt_vol = filt_vol &#8211; 3/28 * ((exp(- (cS / kappa).^2 )) .* (dS));<br />
    clear cS;<br />
    clear dS;</p>
<p>    kernel = zeros(1,1,3); kernel(2) = -1; kernel(3) = 1;<br />
    dU = convn(filt_vol, kernel, &#8216;full&#8217;); dU=dU(:,:,2:size(dU,3)-1,:);<br />
    cU = repmat(sqrt(sum(dU.^2, 4)), [1 1 1 size(dS,4)]);<br />
    filt_vol = filt_vol + 1/28 * ((exp(- (cU / kappa).^2 )) .* (dU));<br />
    clear dU;<br />
    clear cU;</p>
<p>    kernel = zeros(1,1,3); kernel(1) = -1; kernel(2) = 1;<br />
    dD = convn(filt_vol, kernel, &#8216;full&#8217;); dD=dD(:,:,2:size(dD,3)-1,:);<br />
    cD = repmat(sqrt(sum(dD.^2, 4)), [1 1 1 size(dS,4)]);<br />
    filt_vol = filt_vol &#8211; 1/28 * ((exp(- (cD / kappa).^2 )) .* (dD));<br />
    clear dD;<br />
    clear cD;<br />
end<br />
[/cc]</p>
<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.<br />
[cc lang="python"]<br />
def aniso(v, kappa=-1, N=1):</p>
<p>        if kappa == -1:<br />
                kappa = prctile(v, 40)</p>
<p>        vf = v.copy()</p>
<p>        for ii in range(N):<br />
                dE = -vf + roll(vf,-1,0)<br />
                dW = vf &#8211; roll(vf,1,0)</p>
<p>                dN = -vf + roll(vf,-1,1)<br />
                dS = vf &#8211; roll(vf,1,1)</p>
<p>                if len(v.shape) > 2:<br />
                        dU = -vf + roll(vf,-1,2)<br />
                        dD = vf &#8211; roll(vf,1,2)</p>
<p>                vf = vf + \<br />
                        3./28. * ((exp(- (abs(dE) / kappa)**2 ) * dE) &#8211; (exp(- (abs(dW) / kappa)**2 ) * dW)) + \<br />
                        3./28. * ((exp(- (abs(dN) / kappa)**2 ) * dN) &#8211; (exp(- (abs(dS) / kappa)**2 ) * dS))<br />
                if len(v.shape) > 2:<br />
                        vf += 1./28. * ((exp(- (abs(dU) / kappa)**2 ) * dU) &#8211; (exp(- (abs(dD) / kappa)**2 ) * dD)) </p>
<p>        return vf<br />
[/cc]</p>
]]></content:encoded>
			<wfw:commentRss>http://mri.brechmos.org/2010/01/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/pulse-sequence-diagrammer/</link>
		<comments>http://mri.brechmos.org/2010/01/pulse-sequence-diagrammer/#comments</comments>
		<pubDate>Mon, 18 Jan 2010 13:59:53 +0000</pubDate>
		<dc:creator>craig</dc:creator>
				<category><![CDATA[Matlab]]></category>
		<category><![CDATA[MRI]]></category>
		<category><![CDATA[Code]]></category>

		<guid isPermaLink="false">http://mri.brechmos.org/?p=500</guid>
		<description><![CDATA[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 &#8230; <a href="http://mri.brechmos.org/2010/01/pulse-sequence-diagrammer/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<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/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/noise-in-mri-magnitude-data/</link>
		<comments>http://mri.brechmos.org/2010/01/noise-in-mri-magnitude-data/#comments</comments>
		<pubDate>Fri, 15 Jan 2010 04:53:00 +0000</pubDate>
		<dc:creator>craig</dc:creator>
				<category><![CDATA[Matlab]]></category>
		<category><![CDATA[MRI]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[Code]]></category>

		<guid isPermaLink="false">http://mri.brechmos.org/?p=355</guid>
		<description><![CDATA[Background Magnitude MRI data has Rician noise distribution by definition ((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 &#8230; <a href="http://mri.brechmos.org/2010/01/noise-in-mri-magnitude-data/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<h2>Background</h2>
<p>Magnitude MRI data has<a href="http://en.wikipedia.org/wiki/Rice_distribution"> Rician noise distribution</a> by definition ((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)).  It comes about because two channels each with Gaussian noise are squared and added together ((R M Henkelman, “Measurement of signal intensities in the presence of noise in MR images,” Medical Physics 12, no. 2 (April 1985): 232-233.)).  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 $latex y_e(t_i) = \sqrt{ \left[y(t_i) + e_1 \right]^2 + e_2^2 }$, where $latex y$ is the true signal, and $latex e_1$ and $latex e_2$ are random numbers from a Gaussian distribution with zero mean and standard deviation $latex \sigma$.  The standard deviation, $latex \sigma$, 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 $latex y$.</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>
<p>[cc lang="matlab"]</p>
<p>%  Setup the initial variables<br />
rho = 100;<br />
t2 = 80; % in ms<br />
te = 10:10:320;  % in ms</p>
<p>%  Create a T2 decay curve<br />
y = rho * exp(-te / t2 );</p>
<p>%  Define the noise to be 5% of the signal<br />
s = 5;</p>
<p>%  Create the two Gaussian random variable vectors<br />
e1 = s * randn(size(y));<br />
e2 = s * randn(size(y));</p>
<p>%  Now create the new, noisy decay curve.<br />
y_e = sqrt( (y+e1).^2 + (e2).^2 );</p>
<p>[/cc]</p>
<h3>Python</h3>
<p>The Python version is quite similar.</p>
<p>[cc lang="python"]</p>
<p>from __future__ import division</p>
<p>#  Setup the initial variables<br />
rho = 100<br />
t2 = 80 # in ms<br />
te = r_[10:330:10] # in ms</p>
<p>#  Create a T2 decay curve<br />
y = rho * exp( -te / t2 )</p>
<p>#  Define the noise to be 5% of the signal<br />
s = 5;</p>
<p>#  Create the two Gaussian random variable vectors<br />
e1 = normal(0, 5, y.shape)<br />
e2 = normal(0, 5, y.shape)</p>
<p>#  Now create the new, noisy decay curve.<br />
y_e = sqrt( (y+e1)**2 + (e2)**2 );</p>
<p>[/cc]</p>
<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 [cci lang="python"]from __future__ import divison[/cci].</li>
<li>To define [cci lang="python"]te[/cci] 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 [cci lang="python"]random[/cci] that could be used.  Instead I used the Numpy [cci lang="python"]normal[/cci] instead as I can pass in the shape parameter.</li>
</ol>
]]></content:encoded>
			<wfw:commentRss>http://mri.brechmos.org/2010/01/noise-in-mri-magnitude-data/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

