% Euvis AWG Example
% Using Matlab to generate a User Defined File, which can be downloaded via the GUI to your module
% Example: Two-tone Waveform 
% with added noise and then Blackman-windowed
%
% Adapted from the example given in the Matlab Help contents for the FFT function
% for the Euvis Arbitrary Waveform Generator
%
% A common use of Fourier transforms is to find the frequency components of a signal 
% buried in a noisy time domain signal. 
% Consider a signal comprised of two sinusoids at frequencies f1 and f2
% corrupted with zero-mean random noise
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% optional housekeeping
close all;
clear;
% end of housekeeping section



%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Calculate and Generate data + noise
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% Please note that you should enter your actual sampling rate and the two desired frequency tones


Fs = 8e9;			% Sampling frequency 8 Gsps
T = 1/Fs;			% Sample time
L = 64*2^12;			% Data Length (length of signal). Because fft will be used later, choose a power of 2
t = (0:L-1)*T;			% Time vector


f0 = 1000e6;			% center frequency 1 GHz
d = 10e6;			% delta from center 10 MHz
f1 = f0-d;			% each tone is delta away from center
f2 = f0+d;


x = sin(2*pi*t*f1) + sin(2*pi*t*f2);	% Sum of a the scaled sinusoids at frequencies f1 and f2
%w = ones(L,1);				% for no windowing (rectangular window)
w = blackman(L);			% use a Blackman window if you have the Signal Processing Toolbox
y = (x + 0.5*randn(size(t))).*w';	% Sinusoids plus Additive Gaussian noise
%y = x.*w';				% or, Sinusoids without added noise


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Generate markers
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

marker1 = round( t/max(t) );					% marker 1 is active for the second half of the data length
marker2 = mod ( round( 2*t/max(t) ) , 2 );			% marker 2 is active in the middle half
marker3 = mod ( round( 4*t/max(t) ) , 2 );			% marker 3 is active twice
marker = marker1 + bitshift(marker2,1) + bitshift(marker3,2);	% concatenate the markers with one bit for each



%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Write the data to a file for use in the AWG GUI
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

fullscale = hex2dec('FD0');					% data amplitude is from 0 to FFF for all AWG modules, use slightly less to avoid overrun
z = round( fullscale * (y - min(y)) / (max(y)-min(y)) );	% shift the data to integer values from 0 to fullscale
udacontents = [z; marker; (0:L-1)];				% data, markers, and index into data
fid = fopen ('C:\Program Files\Euvis\AWG\__WF\userdef_from_matlab.uda','wt');

count = fprintf(fid, ';---- User-Defined file generated in Matlab\n');
count = fprintf(fid, '#type=5\n');				% type 5 means there is a 2nd column for marker info
count = fprintf(fid, '#hex=1\n');				% data is in hexadecimal format
count = fprintf(fid, ';---- User-Defined file data and marker contents\n');
count = fprintf(fid, '%03X %1o\t; [%6d]\n', udacontents);
fclose (fid);


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Calculate Fourier Transform if you have the Signal Processing Toolbox
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% It is difficult to identify the frequency components by looking at the original signal. 
% Converting to the frequency domain, the discrete Fourier transform of the 
% noisy signal y is found by taking the fast Fourier transform (FFT):

NFFT = 2^nextpow2(L); 		% Next power of 2 from data length for faster FFT
Y = fft(y,NFFT)/L;		% Fast Fourier Transform of windowed noisy signal
f = Fs/2*linspace(0,1,NFFT/2+1);	% generate a frequency index



%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Plot in Matlab to get an idea of an idealized output
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% Plot time domain
figure(1);
%plot(1e6*t,y);				% if L is not too large, you can plot all of the data
plot(1e6*t(1:512:L),y(1:512:L));	% for faster results, plot samples of the data if L is large
title('Signal Corrupted with Zero-Mean Random Noise')
xlabel('time (microseconds)')
axis tight;


% Plot single-sided amplitude spectrum
figure(2);
semilogy(f/1e9,2*abs(Y(1:NFFT/2+1))) 	% plot with logarithmic amplitude the upper sideband
%ylim([1e-8 1])
axis([ 0, Fs/2e9, 1e-4, 1 ]);		% force x axis to range from 0 to Nyquist
title('Single-Sided Amplitude Spectrum of y(t)')
xlabel('Frequency (GHz)')
ylabel('|Y(f)|')



% Now, set up your Euvis AWG module with 
% a clock source, a power supply, and a cooling fan to blow air across the module
%
% From the Euvis AWG GUI, select User-Defined Waveform, and open the
% user defined file just created. 
%
% Ensure that the signature frequency is set to match your clock frequency.
%
% Click Download, wait for the download to complete, and then click Restart.
%
