# The fftn processing problem for odd number of points

9 views (last 30 days)

Answered: Abhimenyu on 7 Jun 2024
In the process of solving partial differential equations by spectral methods it is common to perform fft operations with even number of points, for some reason I need to perform fftn operations with odd number of points, I understand that it seems inaccurate to use fftn(c) directly, can anyone help me?
Nx=192;
Nz=192*4;
Lx=0.176;
Lz=4*Lx;
dx=Lx/Nx;
dz=Lz/Nz;
x=0+(0:Nx-1)*dx;%This is an even number of points
z=0+(0:Nz-1)*dz;
[X,Z]=meshgrid(x,z);
wint=Lx/16;
xigema0=cos(2*pi/Lx*X);
c1=1/2*(1-tanh(2*Z/wint));
c2=1/2*(1+tanh(2*(Z-2*Lx-(0.5e-4)*Lx*xigema0)/wint));
c3=1/2*(1-tanh(2*(Z-4*Lx)/wint));
c=(0<=Z & Z<Lx).*c1+(Lx<=Z & Z<3*Lx).*c2+(3*Lx<=Z & Z<=4*Lx).*c3;
clear xigema xigema0 c1 c2 c3
c_t=fftn(c);
As shown in the figure, I need the initial concentration to be 0.50 at both the left and right ends, so I need to perform the following Fourier operation procedure.

Abhimenyu on 7 Jun 2024
Hello,
I understand that you want to perform the fftn operation with an odd number of points. This can be done using zero-padding to increase the number of points to the next even number, perform the FFT, and then adjust the results accordingly.
Please follow this MATLAB example code to understand how to handle FFT operations with an odd number of points by using padding:
Nx = 193; % Example odd number of points
Nz = 193 * 4; % Example odd number of points (can be even or odd)
Lx = 0.176;
Lz = 4 * Lx;
dx = Lx / Nx;
dz = Lz / Nz;
x = 0 + (0:Nx-1) * dx;
z = 0 + (0:Nz-1) * dz;
[X, Z] = meshgrid(x, z);
wint = Lx / 16;
xigema0 = cos(2 * pi / Lx * X);
c1 = 1 / 2 * (1 - tanh(2 * Z / wint));
c2 = 1 / 2 * (1 + tanh(2 * (Z - 2 * Lx - (0.5e-4) * Lx * xigema0) / wint));
c3 = 1 / 2 * (1 - tanh(2 * (Z - 4 * Lx) / wint));
c = (0 <= Z & Z < Lx) .* c1 + (Lx <= Z & Z < 3 * Lx) .* c2 + (3 * Lx <= Z & Z <= 4 * Lx) .* c3;
clear xigema xigema0 c1 c2 c3
Determine the Next Even Number of Points: Increment Nx and Nz to the next even numbers (Nxp and Nzp).
% Zero-padding to the next even number of points
Nxp = Nx + 1;
Nzp = Nz + 1;
Zero-Padding: Create a new array cp with the next even number of points and pad the original c with zeros.
cp = zeros(Nzp, Nxp);
cp(1:Nz, 1:Nx) = c;
Perform FFT: Execute the FFT on the zero-padded array using the MATLAB's fftn function as explained by this MATLAB R2024a documentation link: https://www.mathworks.com/help/matlab/ref/fftn.html
% Perform FFT on the padded array
cp_t = fftn(cp);
Truncate the Result: After the FFT operation, truncate the result to the original size to obtain the FFT for the odd-sized array.
% Truncate the FFT result to the original size
c_t = cp_t(1:Nz, 1:Nx);
% Now c_t contains the FFT result for the original odd-sized array
If you need to ensure the initial concentration is 0.50 at both the left and right ends, you can adjust the initial conditions or apply boundary conditions accordingly.
I hope this helps!

### Categories

Find more on Fourier Analysis and Filtering in Help Center and File Exchange

R2021a

### Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!