Add experiment

This commit is contained in:
Ksyer
2024-05-09 16:48:29 +08:00
parent 06b3aa1a6d
commit d7b4d9beb2
7 changed files with 173 additions and 0 deletions
@@ -0,0 +1,47 @@
%%
clc;
clear;
N = 100;
eps = 1e-10;
F_n = dftmtx(N);
for i = 1: N
for j = 1: N
fprintf(string(F_n(i, j)) + ", ");
end
fprintf("\b\b\n");
end
fprintf("\n");
% 检查傅里叶矩阵的正确性
assert(abs(F_n(2, 2) ^ 2 - F_n(2, 3)) < eps);
assert(abs(F_n(2, 2) ^ N - 1) < eps);
%% 检查循环卷积
x = randn(N, 1);
z = randn(N, 1);
p = cconv(x, z, N);
% 检查循环卷积公式的正确性
for i = 1: N
tmp = 0;
for j = 1: N
tmp = tmp + x(j) * z(mod(i-j+N, N)+1);
end
assert(abs(tmp - p(i)) < eps);
end
% 检查 x * z = A(x)z 的正确性
A_x = toeplitz([x(1) fliplr(x(2:end)')], x);
assert(is_vector_equal(p, A_x' * z, eps));
% 检查卷积定理的正确性
x_hat = fft(x);
z_hat = fft(z);
xz_hat = fft(p);
assert(is_vector_equal(x_hat .* z_hat, xz_hat, eps));
@@ -0,0 +1,5 @@
function flg = is_vector_equal(A, B, eps)
diff = abs(A - B);
max_diff = max(diff);
flg = max_diff <= eps;
end