<?xml version="1.0" encoding="UTF-8"?>
<!-- generator="bbPress/1.0.2" -->
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom">
	<channel>
		<title>k-Wave User Forum &#187; Topic: error about &#039;CartInterp&#039; and &#039;DataCast&#039;</title>
		<link>http://www.k-wave.org/forum/topic/error-about-cartinterp-and-datacast</link>
		<description>Support for the k-Wave MATLAB toolbox</description>
		<language>en-US</language>
		<pubDate>Tue, 12 May 2026 23:08:03 +0000</pubDate>
		<generator>http://bbpress.org/?v=1.0.2</generator>
		<textInput>
			<title><![CDATA[Search]]></title>
			<description><![CDATA[Search all topics from these forums.]]></description>
			<name>q</name>
			<link>http://www.k-wave.org/forum/search.php</link>
		</textInput>
		<atom:link href="http://www.k-wave.org/forum/rss/topic/error-about-cartinterp-and-datacast" rel="self" type="application/rss+xml" />

		<item>
			<title>Bradley Treeby on "error about &#039;CartInterp&#039; and &#039;DataCast&#039;"</title>
			<link>http://www.k-wave.org/forum/topic/error-about-cartinterp-and-datacast#post-449</link>
			<pubDate>Thu, 12 Apr 2012 00:19:15 +0000</pubDate>
			<dc:creator>Bradley Treeby</dc:creator>
			<guid isPermaLink="false">449@http://www.k-wave.org/forum/</guid>
			<description>&#60;p&#62;Hi Chao,&#60;/p&#62;
&#60;p&#62;Thanks for your functions, they look great! I did some quick testing and agree they seem to be significantly faster, both for precomputing the triangulation, and for evaluating the interpolation. They also avoid using the &#60;code&#62;tsearch&#60;/code&#62; function used by the original &#60;code&#62;gridDataFast&#60;/code&#62; (which is slowly being phased out of MATLAB), and seem to use a lot less memory than &#60;code&#62;TriScatteredInterp&#60;/code&#62;. So all-in-all, I think they'd make a useful addition to the toolbox. &#60;/p&#62;
&#60;p&#62;A possible modification would be to catch any NANS inside &#60;code&#62;t&#60;/code&#62; which will occur if the evaluation points are outside the grid dimensions. For example, something like&#60;/p&#62;
&#60;p&#62;if any(isnan(t))&#60;br /&#62;
      error('Cartesian sensor coordinates must lie within the grid dimensions');&#60;br /&#62;
end&#60;/p&#62;
&#60;p&#62;Brad.
&#60;/p&#62;</description>
		</item>
		<item>
			<title>huangchao on "error about &#039;CartInterp&#039; and &#039;DataCast&#039;"</title>
			<link>http://www.k-wave.org/forum/topic/error-about-cartinterp-and-datacast#post-448</link>
			<pubDate>Wed, 11 Apr 2012 21:27:45 +0000</pubDate>
			<dc:creator>huangchao</dc:creator>
			<guid isPermaLink="false">448@http://www.k-wave.org/forum/</guid>
			<description>&#60;p&#62;Hi, Dr. Treeby,&#60;/p&#62;
&#60;p&#62;I found that the same interpolation results given by 'gridDataFast' are slightly different from the ones from 'TriScatteredInterp'. And, as you mentioned in the previous post, 'TriScatteredInterp' cannot be used with 'single' or 'gsingle' data type.&#60;/p&#62;
&#60;p&#62;So I modified the 'gridDataFast' so that it will give the exact same interpolation results as 'TriScatteredInterp', and it supports 'DataCast', which means we can use GPUs to accelerate the computation in 3D case if 'CartInterp' is set to be 'linear'.&#60;/p&#62;
&#60;p&#62;In 2D case, the modified version is:&#60;/p&#62;
&#60;p&#62;function [tri, bc] = gridDataFast2D(x, y, xi, yi)&#60;/p&#62;
&#60;p&#62;% enforce x, y, xi, yi to be column vectors&#60;br /&#62;
x = x(:);&#60;br /&#62;
y = y(:);&#60;br /&#62;
xi = xi(:);&#60;br /&#62;
yi = yi(:);&#60;/p&#62;
&#60;p&#62;% triangulize the data&#60;br /&#62;
tri = DelaunayTri(x, y);&#60;/p&#62;
&#60;p&#62;% catch trinagulation error&#60;br /&#62;
if isempty(tri)&#60;br /&#62;
    error('Data cannot be triangulated.');&#60;br /&#62;
end&#60;/p&#62;
&#60;p&#62;% find the nearest triangle and the corresponding Barycentric coordinates&#60;br /&#62;
[t, bc] =  pointLocation(tri,[xi yi]);&#60;/p&#62;
&#60;p&#62;tri = tri(t,:);&#60;/p&#62;
&#60;p&#62;After the 'tri' and 'bc' are computed in 'kspaceFirstOrder_createStorageVariables', the 'sensor_data' can be calculated by sensor_data(:, t_index) = sum(p(sensor.tri) .* sensor.bc, 2) in the kspaceFirstOrder2D.&#60;/p&#62;
&#60;p&#62;Likely, the 3D version is:&#60;/p&#62;
&#60;p&#62;function [tri, bc] = gridDataFast3D(x, y, z, xi, yi, zi)&#60;/p&#62;
&#60;p&#62;% enforce x, y, z, xi, yi, zi to be column vectors&#60;br /&#62;
x = x(:);&#60;br /&#62;
y = y(:);&#60;br /&#62;
z = z(:);&#60;br /&#62;
xi = xi(:);&#60;br /&#62;
yi = yi(:);&#60;br /&#62;
zi = zi(:);&#60;/p&#62;
&#60;p&#62;% triangulize the data&#60;br /&#62;
tri = DelaunayTri(x, y, z);&#60;/p&#62;
&#60;p&#62;% catch trinagulation error&#60;br /&#62;
if isempty(tri)&#60;br /&#62;
    error('Data cannot be triangulated.');&#60;br /&#62;
end&#60;/p&#62;
&#60;p&#62;% find the nearest triangle and the corresponding Barycentric coordinates&#60;br /&#62;
[t, bc] =  pointLocation(tri,[xi yi zi]);&#60;/p&#62;
&#60;p&#62;tri = tri(t,:);&#60;/p&#62;
&#60;p&#62;Similarly, After the 'tri' and 'bc' are computed in 'kspaceFirstOrder_createStorageVariables', the 'sensor_data' can be calculated by sensor_data(:, t_index) = sum(p(sensor.tri) .* sensor.bc, 2) in the kspaceFirstOrder3D.&#60;/p&#62;
&#60;p&#62;Also, I found the modified 'gridDataFast2D' and 'gridDataFast3D' are faster than 'gridDataFast' and 'TriScatteredInterp'. For example, in 2D case, we had:&#60;/p&#62;
&#60;p&#62;tic;&#60;br /&#62;
[tri, bc] = gridDataFast2D(kgrid.x, kgrid.y, sensor.mask(1,:), sensor.mask(2,:));&#60;br /&#62;
i1 = sum(source.p0(tri) .* bc,2);&#60;br /&#62;
toc&#60;br /&#62;
Elapsed time is 1.072967 seconds.&#60;/p&#62;
&#60;p&#62;tic;&#60;br /&#62;
[zi, del_tri] = gridDataFast(kgrid.x, kgrid.y, zeros(kgrid.Nx, kgrid.Ny),  sensor.mask(1,:), sensor.mask(2,:));&#60;br /&#62;
i2 = gridDataFast(kgrid.x, kgrid.y, source.p0,  sensor.mask(1,:), sensor.mask(2,:),  del_tri);&#60;br /&#62;
toc&#60;br /&#62;
Elapsed time is 5.227701 seconds.&#60;/p&#62;
&#60;p&#62;tic;&#60;br /&#62;
F_interp = TriScatteredInterp(reshape(kgrid.x, [], 1), reshape(kgrid.y, [], 1), reshape(zeros(kgrid.Nx, kgrid.Ny), [], 1));&#60;br /&#62;
F_interp.V = reshape(source.p0, [], 1);&#60;br /&#62;
i3 = F_interp(sensor.mask(1, :), sensor.mask(2, :));&#60;br /&#62;
toc&#60;br /&#62;
Elapsed time is 2.090216 seconds.&#60;/p&#62;
&#60;p&#62;In 3D:&#60;/p&#62;
&#60;p&#62;tic;&#60;br /&#62;
[tri, bc] = gridDataFast3D(kgrid.x, kgrid.y, kgrid.z, sensor.mask(1,:), sensor.mask(2,:), sensor.mask(3,:));&#60;br /&#62;
i1 = sum(source.p0(tri) .* bc,2);&#60;br /&#62;
toc&#60;br /&#62;
Elapsed time is 98.273495 seconds.&#60;/p&#62;
&#60;p&#62;tic;&#60;br /&#62;
F_interp = TriScatteredInterp(reshape(kgrid.x, [], 1), reshape(kgrid.y, [], 1), reshape(kgrid.z, [], 1), reshape(zeros(kgrid.Nx, kgrid.Ny, kgrid.Nz), [], 1));&#60;br /&#62;
F_interp.V = reshape(source.p0, [], 1);&#60;br /&#62;
i2 = F_interp(sensor.mask(1,:), sensor.mask(2,:), sensor.mask(3,:));&#60;br /&#62;
toc&#60;br /&#62;
Elapsed time is 135.914373 seconds.&#60;/p&#62;
&#60;p&#62;Best,&#60;br /&#62;
Chao
&#60;/p&#62;</description>
		</item>
		<item>
			<title>huangchao on "error about &#039;CartInterp&#039; and &#039;DataCast&#039;"</title>
			<link>http://www.k-wave.org/forum/topic/error-about-cartinterp-and-datacast#post-392</link>
			<pubDate>Fri, 16 Mar 2012 23:08:14 +0000</pubDate>
			<dc:creator>huangchao</dc:creator>
			<guid isPermaLink="false">392@http://www.k-wave.org/forum/</guid>
			<description>&#60;p&#62;Hi,&#60;/p&#62;
&#60;p&#62;I just found that there may be a mistake in the help document of the optimising k-Wave performance example.&#60;/p&#62;
&#60;p&#62;It says, quote, &#34;Note, the interpolation function used within kspaceFirstOrder2D and kspaceFirstOrder3D does not currently support GPU usage, so the optional input parameter 'CartInterp' should be set to 'nearest' if using a Cartesian sensor mask.&#34; But actually, when calling 'kspaceFirstOrder2D', 'CartInterp' can be set to 'linear' when 'DataCast' is set to 'gsingle', because the 'gridDataFast' used for interpolation in 'kspaceFirstOrder2D' supports both 'single' and 'gsingle'.&#60;/p&#62;
&#60;p&#62;Best,&#60;br /&#62;
Chao
&#60;/p&#62;</description>
		</item>
		<item>
			<title>Bradley Treeby on "error about &#039;CartInterp&#039; and &#039;DataCast&#039;"</title>
			<link>http://www.k-wave.org/forum/topic/error-about-cartinterp-and-datacast#post-187</link>
			<pubDate>Thu, 30 Jun 2011 23:49:41 +0000</pubDate>
			<dc:creator>Bradley Treeby</dc:creator>
			<guid isPermaLink="false">187@http://www.k-wave.org/forum/</guid>
			<description>&#60;p&#62;Hi Chao,&#60;/p&#62;
&#60;p&#62;Unfortunately this is a limitation of the MATLAB function &#60;code&#62;TriScatteredInterp&#60;/code&#62; which is used for linear interpolation within k-Wave. This function only supports doubles. For example, try&#60;/p&#62;
&#60;pre&#62;&#60;code&#62;&#38;gt;&#38;gt; F = TriScatteredInterp(single(rand(100,1)), single(rand(100,1)),
single(rand(100,1)));
??? Error using ==&#38;gt; TriScatteredInterp
The input points must be a double array.&#60;/code&#62;&#60;/pre&#62;
&#60;p&#62;In the 2D case, the k-Wave code checks if the parameter &#60;code&#62;&#38;#39;DataCast&#38;#39;&#60;/code&#62; is used, and if so it switches to using &#60;code&#62;gridDataFast&#60;/code&#62; instead of &#60;code&#62;TriScatteredInterp&#60;/code&#62;. However, &#60;code&#62;gridDataFast&#60;/code&#62; only works in 2D. At this stage, the two options you mention are the best way to avoid this error.&#60;/p&#62;
&#60;p&#62;There are some notes in the documentation &#60;a href=&#34;http://www.k-wave.org/documentation/example_ivp_3D_simulation.php#heading6&#34;&#62;here&#60;/a&#62;.&#60;/p&#62;
&#60;p&#62;Kind regards,&#60;/p&#62;
&#60;p&#62;Brad.
&#60;/p&#62;</description>
		</item>
		<item>
			<title>huangchao on "error about &#039;CartInterp&#039; and &#039;DataCast&#039;"</title>
			<link>http://www.k-wave.org/forum/topic/error-about-cartinterp-and-datacast#post-186</link>
			<pubDate>Thu, 30 Jun 2011 21:13:41 +0000</pubDate>
			<dc:creator>huangchao</dc:creator>
			<guid isPermaLink="false">186@http://www.k-wave.org/forum/</guid>
			<description>&#60;p&#62;Hi, Dr. Cox and Dr. Treeby,&#60;/p&#62;
&#60;p&#62;Now I am trying to run a 3D simulation, and Cartesian 'sensor.mask' is used. The problem is that if 'CartInterp' is set to be 'linear' and 'DataCast' is set to be 'single', then an error will prompt, saying:&#60;/p&#62;
&#60;p&#62;??? Error using ==&#38;gt; subsasgn&#60;br /&#62;
The input points must be a double array.&#60;/p&#62;
&#60;p&#62;Error in ==&#38;gt; kspaceFirstOrder3D at 935&#60;br /&#62;
            F_interp.V = reshape(p, [], 1);&#60;/p&#62;
&#60;p&#62;However, if I used&#60;br /&#62;
'input_args = {'CartInterp', 'linear', 'DataCast','off'}'&#60;br /&#62;
or&#60;br /&#62;
'input_args = {'CartInterp', 'nearest', 'DataCast','single'}',&#60;br /&#62;
then there is no problem in both of these 2 cases.&#60;/p&#62;
&#60;p&#62;I think the culprit may be the 'F_interp.V', which perhaps needs to be cast to 'single', too.&#60;/p&#62;
&#60;p&#62;Chao
&#60;/p&#62;</description>
		</item>

	</channel>
</rss>
