function Y = upfirdn(x,h,varargin)
%UPFIRDN  Upsample, apply a specified FIR filter, and downsample a signal on a GPU.
%   Y = UPFIRDN(X,H)
%   Y = UPFIRDN(X,H,P)
%   Y = UPFIRDN(X,H,P,Q)

%   Copyright 2020-2022 The MathWorks, Inc.

% Validate number of input args.
narginchk(2,4);

% Ensure only data is on the GPU.
if nargin > 2
    [varargin{:}] = gather(varargin{:});
end

%----------------------------------------------------------------------
% Check if input signal and/or filter are on a gpuArray.
if ~isgpuarray(x) && ~isgpuarray(h)
    % Both signal and filter are in-memory arrays, so run in-memory.
    Y = upfirdn(x,h,varargin{:});
    return;
end

% Make sure both signal and filter are gpuArray.
x = gpuArray(x);
h = gpuArray(h);

% Validate input args and define defaults.
[x,h,p,q,Lx,Lh,hCols,nChans,outputIsRow] = signal.internal.upfirdn.upfirdnParser(x,h,varargin);

% Call builtin
Y = signal.internal.gpu.upfirdn(x,h,p,q,Lx,Lh,nChans,hCols);

if outputIsRow
    Y = reshape(Y,1,[]);
end
