<?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: k vector for even number of grid points</title>
		<link>http://www.k-wave.org/forum/topic/k-vector-for-even-number-of-grid-points</link>
		<description>Support for the k-Wave MATLAB toolbox</description>
		<language>en-US</language>
		<pubDate>Wed, 13 May 2026 06:51:06 +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/k-vector-for-even-number-of-grid-points" rel="self" type="application/rss+xml" />

		<item>
			<title>mat.simurda on "k vector for even number of grid points"</title>
			<link>http://www.k-wave.org/forum/topic/k-vector-for-even-number-of-grid-points#post-4959</link>
			<pubDate>Fri, 23 Jan 2015 11:35:29 +0000</pubDate>
			<dc:creator>mat.simurda</dc:creator>
			<guid isPermaLink="false">4959@http://www.k-wave.org/forum/</guid>
			<description>&#60;p&#62;Hi Brad,&#60;/p&#62;
&#60;p&#62;thank you very much for your reply. Yes, your explanation helped me to understand the problem. Have a nice day and once again, thank you.&#60;/p&#62;
&#60;p&#62;Best regards&#60;br /&#62;
Matej
&#60;/p&#62;</description>
		</item>
		<item>
			<title>Bradley Treeby on "k vector for even number of grid points"</title>
			<link>http://www.k-wave.org/forum/topic/k-vector-for-even-number-of-grid-points#post-4947</link>
			<pubDate>Thu, 22 Jan 2015 18:56:47 +0000</pubDate>
			<dc:creator>Bradley Treeby</dc:creator>
			<guid isPermaLink="false">4947@http://www.k-wave.org/forum/</guid>
			<description>&#60;p&#62;Hi Matej,&#60;/p&#62;
&#60;p&#62;Thanks for your post. If you define the wavenumbers asymmetrically, the band limited interpolant (BLI) will have both real and complex parts (this is point being made in the caption of Fig. 3.1 in the book you mention). The exact form of the BLI is given in Eq. 28 in &#60;a href=&#34;http://www.k-wave.org/papers/2011-Treeby-JASA.pdf&#34;&#62;this paper&#60;/a&#62;. &#60;/p&#62;
&#60;p&#62;Computationally you can check this:&#60;/p&#62;
&#60;pre&#62;&#60;code&#62;clear all

% define x grid
dx = 1;
Nx = 20;
x = (0:Nx-1);

% define delta function
f = zeros(1, Nx);
f(1) = 1;

% define set of wavenumbers
k = (-pi/dx) : (2*pi/(dx*Nx)) : (pi/dx-2*pi/(dx*Nx));

% define fine grid for displaying BLI
x_fine = 0:dx/10:(Nx-1)*dx;

% calculate Fourier coefficients
func_k = ifftshift(fft(f)) / Nx;

% calculate BLI by summing sinusoids
BLI_fft = sum( func_k * exp(-1i * k.&#38;#39; * x_fine), 1);

% calculate BLI using closed form from JASA paper
BLI_exact = 1/Nx * (1i + cot(x_fine*pi/(dx*Nx))) .* sin(x_fine*pi/dx);

% plot BLI
figure;
subplot(2, 1, 1)
plot(x_fine, real(BLI_fft), &#38;#39;k-&#38;#39;);
hold on;
plot(x_fine, real(BLI_exact), &#38;#39;b.&#38;#39;);
plot(x, f, &#38;#39;ro&#38;#39;);
xlabel(&#38;#39;x&#38;#39;);
ylabel(&#38;#39;real(f(x))&#38;#39;);
title(&#38;#39;Unsymmetric wavenumbers&#38;#39;);

subplot(2, 1, 2)
plot(x_fine, imag(BLI_fft), &#38;#39;k-&#38;#39;);
hold on;
plot(x_fine, imag(BLI_exact), &#38;#39;b.&#38;#39;);
plot(x, zeros(size(x)), &#38;#39;ro&#38;#39;);
xlabel(&#38;#39;x&#38;#39;);
ylabel(&#38;#39;imag(f(x))&#38;#39;);&#60;/code&#62;&#60;/pre&#62;
&#60;p&#62;In this example, the BLI is correct in that it matches the function exactly at all of the grid points. However, there is a sinusoidal variation in the complex part that would give the derivative incorrectly. However, from Trefethen's example in Fig. 3.1, you'll note that taking the real part of the interpolant exp(i*pi*x/h) gives the correct result. This is exactly what happens in k-Wave - the real part is taken after each spectral derivative is computed. &#60;/p&#62;
&#60;p&#62;Again, you can check this gives the same thing computationally:&#60;/p&#62;
&#60;pre&#62;&#60;code&#62;clear all;

% define grid parameters (Nx must be even)
Nx   = 10;
dx   = 2*pi/Nx;
x    = (0:Nx-1)*dx;

% define function
%   1: sinusoid
%   2: sawtooth
func = 1;

switch func
    case 1
        % sinusoid
        f    = sin(4*x);
        dfdx = 4*cos(4*x);
    case 2
        % saw tooth
        f = ones(1, Nx);
        f(2:2:end) = 0;
        dfdx = 0;
end

% define wavenumbers as they are in k-Wave
kx = (2*pi/dx) .* (-Nx/2:Nx/2-1)/Nx;

% put wavenumbers in correct order for FFT
kx = ifftshift(kx);

% compute spectral derivative
dfdx_spec = real(ifft(1i*kx .* fft(f)));

% define Trefethyn wavenumbers (this sets the Nyquist value to zero)
kx_tref = [0:Nx/2-1 0 -Nx/2+1:-1];

% compute spectral derivative
dfdx_tref = ifft(1i*kx_tref .* fft(f));

% plot derivative
figure;
subplot(2, 1, 1);
plot(x, f, &#38;#39;k--&#38;#39;);
hold on;
plot(x, dfdx, &#38;#39;k-&#38;#39;);
plot(x, dfdx_spec, &#38;#39;b.&#38;#39;);
plot(x, dfdx_tref, &#38;#39;rd&#38;#39;);
legend(&#38;#39;f&#38;#39;, &#38;#39;dfdx - analytical&#38;#39;, &#38;#39;dfdx - k-Wave&#38;#39;, &#38;#39;dfdx - Trefethyn&#38;#39;);
xlabel(&#38;#39;x&#38;#39;);
ylabel(&#38;#39;f&#38;#39;);

subplot(2, 1, 2);
plot(x, dfdx - dfdx_spec, &#38;#39;b-&#38;#39;);
hold on;
plot(x, dfdx - dfdx_tref, &#38;#39;r--&#38;#39;);
xlabel(&#38;#39;x&#38;#39;);
ylabel(&#38;#39;error&#38;#39;);
legend(&#38;#39;k-Wave&#38;#39;, &#38;#39;Trefethyn&#38;#39;);&#60;/code&#62;&#60;/pre&#62;
&#60;p&#62;Hope that helps,&#60;/p&#62;
&#60;p&#62;Brad.
&#60;/p&#62;</description>
		</item>
		<item>
			<title>mat.simurda on "k vector for even number of grid points"</title>
			<link>http://www.k-wave.org/forum/topic/k-vector-for-even-number-of-grid-points#post-4933</link>
			<pubDate>Thu, 08 Jan 2015 15:04:14 +0000</pubDate>
			<dc:creator>mat.simurda</dc:creator>
			<guid isPermaLink="false">4933@http://www.k-wave.org/forum/</guid>
			<description>&#60;p&#62;Good day,&#60;/p&#62;
&#60;p&#62;thank you very much for the impressive K-wave toolbox.&#60;/p&#62;
&#60;p&#62;I believe I have found an error in your source code:&#60;/p&#62;
&#60;p&#62;While defining wave vector kgrid.kx_vec/ky_vec/kz_vec for even number of grid points you don't set the wave number for index -N/2 to zero.&#60;/p&#62;
&#60;p&#62;...&#60;br /&#62;
if rem(Nx, 2) == 0&#60;br /&#62;
                % grid dimension has an even number of points&#60;br /&#62;
                nx = ((-Nx/2:Nx/2-1)/Nx).';&#60;br /&#62;
            else&#60;br /&#62;
...&#60;/p&#62;
&#60;p&#62;This treats the highest wavenumber asymmetrically (see L.N. Trefethen, Spectral methods in MATLAB, Chapter 3).&#60;/p&#62;
&#60;p&#62;Best regards&#60;br /&#62;
Matej
&#60;/p&#62;</description>
		</item>

	</channel>
</rss>
