# MATLAB Spectrum Example¶

In this example the DPD80 is used measure the frequency spectrum (0 - 40 MHz) of the incident light. To remove noise in your signal, this example captures NAVG arrays of data, fourier transforms them seperately and averages them together.

%Create an instance of the MATLAB riDevice and open it.
device = riDevice();

%To specify which device is opened specify your serial number:
%device = riDevice('FXXXX-BXXXX-XX')
device.set_antialias(true);
device.set_highgain(true);

BW = 100; % Resolution Bandwidth of the spectrum in Hz.
NAVG = 1; % Number of spectrums to average together.
RATE = 80e6; % sample rate of the DPD80
NFT = floor(RATE / BW); % Number of samples transform in each average.

PSD = zeros(1, floor(NFT/2) + 1 );
fs = 0:BW:BW*floor(NFT/2);

%Convert to uW. A and B are found on your calibration sheet.
if (highgain == true)
A =  4.62e-03;
B = -2.48e-00;
else
A =  1.84e-02;
B = -1.04e+01;
end

%Get the resposivity at the wavelength of interest
R = device.get_relative_responsivity(800);

%Collect NAVG sets of data, fourier transform and average
for i = 1:NAVG
data_micro_watts = double( device.get_raw_data(NFT) ) * A * R + B;
fft_full = fft(data_micro_watts);
fft_half = fft_data_full(1:NFT/2+1);
psd_i = real(fft_half .* conj(fft_half)) / double(NFT * RATE) * 2;
PSD = PSD +  psd_i / double(NAVG);

if (mod(i,5) == 0)
sprintf('FFTing section: %d', i)
end
end
microWatts_per_root_Hz = PSD .^ .5;

%Plot the spectrum
semilogy(fs / 1e6, microWatts_per_root_Hz * 1e6);
title('DPD80 Spectrum')
ylabel('PSD (pW / $$\sqrt{\mathrm{Hz}}$$','Interpreter','latex')
xlabel('Frequency (MHz)')

%close the connection to your device
device.close();