Skip to content

Instantly share code, notes, and snippets.

Created January 2, 2013 01:46
Show Gist options
  • Save anonymous/4431523 to your computer and use it in GitHub Desktop.
Save anonymous/4431523 to your computer and use it in GitHub Desktop.
function addsines()
ampScale = 5;
%% Build the GUI
f = figure(1);
ampOneSlide = uicontrol('Style', 'slider', ...
'Units', 'normalized', ...
'Position', [0.1 0.8, 0.1, 0.05], ...
'Callback', @updateSliders);
ampOneEdit = uicontrol('Style', 'edit', ...
'Units', 'normalized', ...
'Position', [0.2, 0.8, 0.1, 0.05], ...
'String', '1.0', ...
'Callback', @update);
phaseOneSlide = uicontrol('Style', 'slider', ...
'Units', 'normalized', ...
'Position', [0.1 0.75, 0.1, 0.05], ...
'Callback', @updateSliders);
phaseOneEdit = uicontrol('Style', 'edit', ...
'Units', 'normalized', ...
'Position', [0.2, 0.75, 0.1, 0.05], ...
'String', '0', ...
'Callback', @update);
ampTwoSlide = uicontrol('Style', 'slider', ...
'Units', 'normalized', ...
'Position', [0.35 0.8, 0.1, 0.05], ...
'Callback', @updateSliders);
ampTwoEdit = uicontrol('Style', 'edit', ...
'Units', 'normalized', ...
'Position', [0.45, 0.8, 0.1, 0.05], ...
'String', '1.0', ...
'Callback', @update);
phaseTwoSlide = uicontrol('Style', 'slider', ...
'Units', 'normalized', ...
'Position', [0.35 0.75, 0.1, 0.05], ...
'Callback', @updateSliders);
phaseTwoEdit = uicontrol('Style', 'edit', ...
'Units', 'normalized', ...
'Position', [0.45, 0.75, 0.1, 0.05], ...
'String', '0', ...
'Callback', @update);
ampSumSlide = uicontrol('Style', 'slider', ...
'Units', 'normalized', ...
'Position', [0.6 0.8, 0.1, 0.05], ...
'Callback', @updateSliders);
ampSumEdit = uicontrol('Style', 'edit', ...
'Units', 'normalized', ...
'Position', [0.7, 0.8, 0.1, 0.05], ...
'String', '0', ...
'Callback', @update);
phaseSumSlide = uicontrol('Style', 'slider', ...
'Units', 'normalized', ...
'Position', [0.6 0.75, 0.1, 0.05], ...
'Callback', @updateSliders);
phaseSumEdit = uicontrol('Style', 'edit', ...
'Units', 'normalized', ...
'Position', [0.7, 0.75, 0.1, 0.05], ...
'String', '0', ...
'Callback', @update);
sigOneLabel = uicontrol('Style', 'text', ...
'Units', 'normalized', ...
'Position', [0.1 0.9, 0.2, 0.05], ...
'String', 'Signal 1');
sigTwoLabel = uicontrol('Style', 'text', ...
'Units', 'normalized', ...
'Position', [0.35 0.9, 0.2, 0.05], ...
'String', 'Signal 2');
sigSumLabel = uicontrol('Style', 'text', ...
'Units', 'normalized', ...
'Position', [0.6 0.9, 0.2, 0.05], ...
'String', 'Sum comparison');
ampLabel = uicontrol('Style', 'text', ...
'Units', 'normalized', ...
'Position', [0.85 0.8, 0.1, 0.05], ...
'String', 'Amplitude');
phaseLabel = uicontrol('Style', 'text', ...
'Units', 'normalized', ...
'Position', [0.85 0.75, 0.1, 0.05], ...
'String', 'Phase');
inputAxes = axes('Position', [0.1, 0.45, 0.8, 0.25]);
sumAxes = axes('Position', [0.1, 0.1, 0.8, 0.25]);
update();
%% Callback for the sliders - Set the text boxes to match the sliders
function updateSliders(varargin)
set(ampOneEdit, 'String', num2str(ampScale * get(ampOneSlide, 'Value')));
set(phaseOneEdit, 'String', num2str(2*pi * get(phaseOneSlide, 'Value')));
set(ampTwoEdit, 'String', num2str(ampScale * get(ampTwoSlide, 'Value')));
set(phaseTwoEdit, 'String', num2str(2*pi * get(phaseTwoSlide, 'Value')));
set(ampSumEdit, 'String', num2str(ampScale * get(ampSumSlide, 'Value')));
set(phaseSumEdit, 'String', num2str(2*pi * get(phaseSumSlide, 'Value')));
update();
end
%% Function which is triggered by callbacks and updates the drawing
function update(varargin)
% Set the sliders to match the text boxes
set(ampOneSlide, 'Value', str2double(get(ampOneEdit, 'String')) / ampScale);
set(phaseOneSlide, 'Value', str2double(get(phaseOneEdit, 'String')) / (2*pi));
set(ampTwoSlide, 'Value', str2double(get(ampTwoEdit, 'String')) / ampScale);
set(phaseTwoSlide, 'Value', str2double(get(phaseTwoEdit, 'String')) / (2*pi));
set(ampSumSlide, 'Value', str2double(get(ampSumEdit, 'String')) / ampScale);
set(phaseSumSlide, 'Value', str2double(get(phaseSumEdit, 'String')) / (2*pi));
t = linspace(0, 2*pi, 1000);
theta = 3; % Just use a fixed frequency
amp1 = str2double(get(ampOneEdit, 'String'));
amp2 = str2double(get(ampTwoEdit, 'String'));
phi1 = str2double(get(phaseOneEdit, 'String'));
phi2 = str2double(get(phaseTwoEdit, 'String'));
thetaSum = theta;
ampSum = str2double(get(ampSumEdit, 'String'));
phiSum = str2double(get(phaseSumEdit, 'String'));
x1 = amp1 * sin(t * theta + phi1);
x2 = amp2 * sin(t * theta + phi2);
s = ampSum * sin(t * thetaSum + phiSum);
maxamp = max(amp1 + amp2, 1);
plot(inputAxes, t, x1, t, x2);
axis(inputAxes, [0, 2*pi, -maxamp, maxamp]);
plot(sumAxes, t, x1 + x2, 'c', t, s, 'r');
axis(sumAxes, [0, 2*pi, -maxamp, maxamp]);
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment