function [x_hat] = DFTinv(a, b, M, N)
% Calculate the inverse discrete fourier transform.
% Author: Jonathan Newnham
    n = 0:(N-1);
    m = 0:(M-1);
    nm = n' * m; % N by M matrix of n * m
    w0 = 2 * pi / N;
    % perform an inverse transform
    x_hat = -a(1)/2 + a * cos(nm' * w0) + b * sin(nm' * w0);
end