# Homework 0 (Discrete Fourier Transform) 以及

I have some questions:
(1)
for i, j in pixels:

for i, j in pixels:

(2)
for i, j in pixels:
pixels[i, j] = xxx
for i, j in pixels:
pixels[(i + offset)%size, (j + offset)%size] = xxx

(3)
pixels = ti.var(dt=ti.f32, shape=(size, size))

DFT code

``````# Follow https://homepages.inf.ed.ac.uk/rbf/HIPR2/fourier.htm

import taichi as ti
from PIL import Image
import numpy as np
from math import pi

ti.init(arch=ti.gpu)

size = 512

image = Image.open(path).convert('LA')
# resize to square image
image = image.resize([size, size])
data = np.asarray(image)
data = np.cast[np.float32](data)
# Convert to grey value within [0, 1]
return data[:, :, 0] / data[:, :, 1]

def show_image(data, scale=True):
if scale:
data = data / np.max(data)
data = data * 255.0
c = np.ones_like(data) * 255.0
data = np.stack([data, c], -1)
data = np.cast[np.uint8](data)
img = Image.fromarray(data, 'LA')
img.show()

pixels = ti.var(dt=ti.f32, shape=(size, size))
results = ti.var(dt=ti.f32, shape=(size, size))

path = input('image path: ')

@ti.kernel
def fourier():
# parallel
for k, l in results:
v = ti.Vector([0.0, 0.0])
for i in range(size):
for j in range(size):
center = size // 2
kk = (k + center) % size
ll = (l + center) % size
angle = -2.0 * pi * (kk * i + ll * j) / float(size)
p = ti.Vector([ti.cos(angle), ti.sin(angle)])
v += pixels[i, j] * p
center = size // 2
results[k, l] = ti.log(1.0 + v.norm())

fourier()
data = results.to_numpy()
show_image(data)
``````

Lenna的结果

2 Likes

func 会强制内联所以目测是一样的。
ref： GAMES201 第一节课 直播录像 | 并行循环 49:34

func 会强制内联所以目测是一样的。
ref： GAMES201 第一节课 直播录像 | 并行循环 49:34