The fftn processing problem for odd number of points
7 次查看(过去 30 天)
显示 更早的评论
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);
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/836200/image.png)
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.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/836205/image.png)
0 个评论
回答(1 个)
Abhimenyu
2024-6-7
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.
% Pad 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!
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Fourier Analysis and Filtering 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!