考虑以下代码:
% DTF vs FFT
%% Example 1 N = 64
close all
clear
clc
eval_dft_vs_fft(64);
%% Example 2 N = 512
close all
clear
eval_dft_vs_fft(512);
%% Example 3 N = 4096
close all
clear
eval_dft_vs_fft(4096);
function [t_DFT,t_FFT, RMSE_FFT, RMSE_DFT] = eval_dft_vs_fft(N)
% generate a arrray of random, complex numbers
x = complex(rand(1, N), rand(1,N));
tic % begin time measurement for the DFT calculation
x_DFT = IDFT(DFT(x)); % Determine the DFT and IDFT result
t_DFT = toc; % end time measurement
tic % begin time measurement for the FFT calculation
x_FFT = ifft(fft(x)); % Determine the FFT and IFFT result
t_FFT = toc; % end time measurement
% calculate the RMS Error of the DTF
mean = sum(abs(x - x_DFT).^2)/N;
RMSE_DFT = sqrt(mean);
% calculate the RMS Error of the FFT
mean = sum(abs(x - x_FFT).^2)/N;
RMSE_FFT = sqrt(mean);
disp("Number of elements N = " + N)
disp(" ")
disp("Calculation Time DTF = " + t_DFT)
disp("Calculation Time FFT = " + t_FFT)
disp(" ")
disp("RMS Error DTF = " + RMSE_DFT)
disp("RMS Error FFT = " + RMSE_FFT)
fprintf('n---------------nn')
end
function x = IDFT(X)
N = length(X);
x = zeros(1, N);
for n=0:N-1
x_1 = 0;
for k = 0:N-1
x_1 = x_1 + X(k+1) .* exp((1j*2*pi*k*n)/N);
end
x(n+1) = x_1;
end
x = x ./ N;
end
function X = DFT(x)
N = numel(x);
X = zeros(1, N);
for k=0:N-1
X_1 = 0;
for n = 0:N-1
X_1 = X_1 + x(n+1) .* exp(-(1j*2*pi*k*n)/N);
end
X(k+1) = X_1;
end
end
其目的是比较DFT和FFT的计算时间以及它们的均方根误差。我在命令窗口中没有错误,但disp语句没有出现在任何地方?
我在命令窗口看到的是这个;
列1到22:
142 181 173 162 165 178 96 175 166 96 165 172 165 173 173 174 180 179 96 142 142 96 125
61 32
我对Octave很陌生,所以任何帮助都很感激。
函数disp不允许混合文本和值。这就是为什么输出是一个数字列表(可能是代码、字符或其他东西)的原因。你可以分开disp("Number of elements N = "), disp(N)
产生两行或使用其他函数printfprintf("Number of elements N = %d", N)
保持文本和值在同一行。