%
% example taken from
% http://www.mathworks.co.uk/help/distcomp/examples/using-fft-on-the-gpu-for-spectral-analysis.html?prodcode=DM&language=en
%

close all; clear all

sampleFreq = 1000;
sampleTime = 1/sampleFreq;
numSamples = 2^23;
timeVec    = gpuArray( (0:numSamples-1) * sampleTime );
freq1      = 2 * pi * 50;
freq2      = 2 * pi * 120;
signal     = sin( freq1 .* timeVec ) + sin( freq2 .* timeVec );

signal  = signal + 2 * randn( size( timeVec ) );
figure
plot( signal(1:100) );
title( 'Noisy time-domain signal' );

transformedSignal = fft( signal );

powerSpectrum = transformedSignal .* conj(transformedSignal) ./ numSamples;

frequencyVector = sampleFreq/2 * linspace( 0, 1, numSamples/2 + 1 );

figure
plot( frequencyVector, real(powerSpectrum(1:numSamples/2+1)) );
title( 'Power spectral density' );
xlabel( 'Frequency (Hz)' );

whos

