%% Overlap-Add Filtering on the GPU
% This example requires Parallel Computing Toolbox™ software. Refer to GPU Support by Release for a list of supported GPUs.
% Create a signal consisting of a sum of sine waves in white Gaussian additive noise. The sine wave frequencies are 2.5, 5, 10, and 15 kHz. The sampling frequency is 50 kHz.

Fs = 50e3;
t = 0:1/Fs:10-(1/Fs);
x = cos(2*pi*2500*t) + 0.5*sin(2*pi*5000*t) + 0.25*cos(2*pi*10000*t)+ ...
    0.125*sin(2*pi*15000*t) + randn(size(t));

%
% Design a lowpass FIR equiripple filter using designfilt.

d = designfilt('lowpassfir','SampleRate',Fs, ...
    'PassbandFrequency',5500,'StopbandFrequency',6000, ...
    'PassbandRipple',0.5,'StopbandAttenuation',50);
B = d.Coefficients;

% Filter the data on the GPU using the overlap-add method. Put the data on the GPU using gpuArray. Return the output to the MATLAB® workspace using gather and plot the power spectral density estimate of the filtered data.

y = fftfilt(gpuArray(B),gpuArray(x));
periodogram(gather(y),rectwin(length(y)),length(y),50e3)

% Copyright 2018 The MathWorks, Inc.