【问题标题】:Why cannot I see the print statements even tho the code is compiling and not showing any errors为什么即使代码正在编译并且没有显示任何错误,我也看不到打印语句
【发布时间】:2022-06-24 10:51:02
【问题描述】:

考虑这段代码:

% DTF 与 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---------------\n\n')
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 的计算时间以及它们的 RMS 误差。我在命令窗口中没有收到任何错误,但 disp 语句没有出现在任何地方?

我在命令窗口中得到的是这个;

第 1 至 22 列:

142 181 173 162 165 178 96 175 166 96 165 172 165 173 165 174 180 179 96 142 96 125

61 32

我是 Octave 的新手,因此非常感谢任何帮助。

【问题讨论】:

  • 你确定你正在运行这段代码吗?
  • 我怀疑你有一个变量,命名与脚本相同,因此在调用命令时你只是显示变量。让您的脚本使用不同的名称,它会起作用。
  • 这看起来不像是有效的八度代码。您是否有机会从 matlab 移植代码? (我的意思是,它会“运行”,但它可能不会做你认为它正在做的事情......至少不是你运行它的第一次
  • 为了扩展@TasosPapastylianou 所说的内容,MATLAB 在几个版本之前进行了更改,以允许脚本在末尾包含函数,并且脚本可以使用它们。以前,仍然在 Octave 中,函数总是必须在使用之前定义。因此,在脚本中,他们需要位于顶部。您的脚本最后具有功能。所以它很可能是为 Matlab 编写的,并且在 Octave 中,在函数定义之前对脚本中这些函数的任何调用都会产生“未定义”错误。
  • error: 'eval_dft_vs_fft' undefined near line 6, column - 请参阅上面 Nick J 的解释

标签: octave


【解决方案1】:

函数disp 不允许混合文本和值。这就是为什么输出是数字列表(可能是 os 字符或其他代码)的原因。你可以分开 disp("Number of elements N = "), disp(N) 产生两行或使用其他函数作为 printf printf("Number of elements N = %d", N) 将文本和值保持在同一行。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-01-04
    • 1970-01-01
    • 2023-02-17
    • 2021-01-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多