% This script tests for full duplex operation with one USRP radio and one
% host computer.  It uses an SDRu transmitter System object to transmit a
% sine wave to the radio, which then receives the same sine wave and sends
% it back to MATLAB via an SDRu receiver System object.  It plots the power
% spectrum of the received sine wave, changes the sine wave amplitude
% midstream, and plots the new power spectrum.

% Initialize parameters
usrpfdplx.SineAmplitude        = 0.1;
usrpfdplx.SineFrequency        = 60e3;  % Hz
usrpfdplx.ADCSampleRate        = 100e6; % sps
usrpfdplx.USRPDecimationFactor = 512;  % Interpolation factor is identical
usrpfdplx.SineSampleRate       = usrpfdplx.ADCSampleRate / ...
                                   usrpfdplx.USRPDecimationFactor;  % sps
usrpfdplx.SineSampleTime       = 1/usrpfdplx.SineSampleRate;  % sec
usrpfdplx.OutputDataType       = 'double';
usrpfdplx.FrameLength          = 1024;
usrpfdplx.USRPCenterFrequency  = 915e6; % Hz
usrpfdplx.USRPGain             = 30;  % dB
usrpfdplx.TotalFrames          = 4000;  % sec
usrpfdplx.HalfTotalFrames      = usrpfdplx.TotalFrames/2;

% Create a sine wave System object
hSineSource = dsp.SineWave(...
    'Amplitude',       usrpfdplx.SineAmplitude, ...
    'Frequency',       usrpfdplx.SineFrequency, ...
    'SampleRate',      usrpfdplx.SineSampleRate, ...
    'SamplesPerFrame', usrpfdplx.FrameLength, ...
    'OutputDataType',  usrpfdplx.OutputDataType);

% Create SDRu Transmitter and SDRu Receiver System objects.  The
% interpolation factor is identical to the decimation factor.
% Use default IP: 192.168.10.2
hSDRuTx = comm.SDRuTransmitter('192.168.10.2', ...
    'CenterFrequency',     usrpfdplx.USRPCenterFrequency, ...
    'Gain',                usrpfdplx.USRPGain, ...
    'InterpolationFactor', usrpfdplx.USRPDecimationFactor)
hSDRuRx = comm.SDRuReceiver('192.168.10.2', ...
    'CenterFrequency',  usrpfdplx.USRPCenterFrequency, ...
    'Gain',             usrpfdplx.USRPGain, ...
    'DecimationFactor', usrpfdplx.USRPDecimationFactor, ...
    'OutputDataType',   usrpfdplx.OutputDataType, ...
    'FrameLength',      usrpfdplx.FrameLength)

% Create a spectrum scope for FFT viewing
hSpecScope = SpectrumScope(...
    'SpectrumType',        'TwoSided', ...
    'NFFT',                usrpfdplx.FrameLength, ...
    'NormalizedFrequency', false, ...
    'SampleRate',          usrpfdplx.SineSampleRate, ...
    'WindowName',          'Hann', ...
    'YLimits',             [-130 -20], ...
    'NumAverages',         50, ...
    'Position',            figposition([50 30 30 40]));

% Stream processing
% Loop until the example reaches the target number of frames.
frameCounter = 0;
while frameCounter < usrpfdplx.TotalFrames
    if frameCounter == usrpfdplx.HalfTotalFrames
        disp('Resetting sine wave amplitude')
        release(hSineSource);
        hSineSource.Amplitude = 0.001;  % to ensure visible change in plot
    end
    sineWave = step(hSineSource);
    step(hSDRuTx, sineWave);
    [rxSineWave, len] = step(hSDRuRx);
    if len > 0
        % Plot the FFT of the sine wave
        step(hSpecScope, rxSineWave);
    end
    frameCounter = frameCounter + 1;
end
