%% Generate Variable-Frequency Staircase Wave
% Define and plot a single-cycle waveform.

values = -1:0.1:1;
singleCycleWave = ones(100,1) * values;
singleCycleWave = reshape(singleCycleWave,numel(singleCycleWave),1);

plot(singleCycleWave)
xlabel('Index')
ylabel('Amplitude')

%
% Create a wavetable synthesizer, waveSynth, to generate a staircase wave using the single-cycle waveform. Specify a frequency of 10 Hz.

waveSynth = wavetableSynthesizer(singleCycleWave,10);

%
% Create a time scope to visualize the staircase wave generated by waveSynth.

scope = dsp.TimeScope( ...
    'SampleRate',waveSynth.SampleRate, ...
    'TimeSpan',0.1, ...
    'YLimits',[-1.5,1.5], ...
    'TimeSpanOverrunAction','Scroll', ...
    'ShowGrid',true, ...
    'Title','Variable-Frequency Staircase Wave');

%
% Place the wavetable synthesizer in an audio stream loop. Increase the frequency of your staircase wave in 10 Hz increments.

counter = 0;
while (counter < 1e4)
    counter = counter + 1;
    staircaseWave = waveSynth();
    scope(staircaseWave)
    if mod(counter,1000)==0
        waveSynth.Frequency = waveSynth.Frequency + 10;
    end
end

% Copyright 2012 The MathWorks, Inc.