function hFigure = visualizePhaseNoisePSD(psdDB, freqOffset, Fs)
    % Visualize the response of the realized phase noise filter, along with
    % the specified PSD.
    %   Inputs:
    %     psdDB      - PSD level in dB
    %     freqOffset - Frequency offset in Hz
    %     Fs         - Sample rate in Hz
    %   Output:
    %     hFigure    - Handle of the figure on which the realized PSD &
    %                  specified PSD are plotted

    %   Copyright 2023 The MathWorks, Inc.

    [num, den, ~, ~, s] = getPhaseNoiseFilterNumDen(psdDB, freqOffset, Fs);

    if isempty(num)
        % Multi-stage filter-synthesizer system. Stitch together the
        % overall realized PSD from the frequency response of individual
        % filters.

        f = [];
        h = [];
        for k = 1:s.NumFilters
            fRange = s.Filter{k}.FrequencyRange;
            Fs = 2 * diff(fRange);
            % Adjust the numerator to account for the AWGN noise level, which
            % always has a variance of 1.  The power output of the randn function,
            % in dBW/Hz, is scaled by Fs.
            nm = s.Filter{k}.Coeffs / sqrt(Fs);
            nmLen = length(nm);
            [hTmp, fTmp] = freqz(nm, 1, nmLen, Fs);
            fTmp = fTmp + fRange(1);
            f = [f; fTmp]; %#ok<AGROW>
            h = [h; hTmp]; %#ok<AGROW>
        end
    else
        % Single filter

        % Adjust the numerator to account for the AWGN noise level, which
        % always has a variance of 1.  The power output of the randn function,
        % in dBW/Hz, is scaled by Fs.
        num = num / sqrt(Fs);
        % Number of frequency points
        n = max([length(num), length(den), 8192]);
        [h, f] = freqz(num, den, n, Fs);
    end

    hFigure = figure('Name', 'Phase Noise PSD', 'Tag', 'PN_PSD_Visualization');
    semilogx(gca(hFigure), f, 20*log10(abs(h)), 'b-', ...
        freqOffset, psdDB, 'ro');
    xLimits = [min(f(1), freqOffset(1)), max(f(end), freqOffset(end))];
    xlim(xLimits);
    grid on;
    legend({'Realized PSD', 'Specified PSD'});
    title(hFigure.CurrentAxes, 'Phase Noise PSD');
    xlabel(hFigure.CurrentAxes, 'Frequency offset (Hz)');
    ylabel(hFigure.CurrentAxes, 'PSD (dBc/Hz)')

end