Merge pull request #7 from ltcptgeneral/improve_comments
Added additional comments for wave generation functions
This commit is contained in:
commit
843084257d
@ -1,15 +1,28 @@
|
|||||||
function x = generate_sine(amplitude, frequency, phase, fs, duration, duty)
|
function x = generate_sine(amplitude, frequency, phase, fs, duration, duty)
|
||||||
%GENERATE_SINE:Arthur Lu returns a matrix of sampled sine wave, where the
|
% GENERATE_SINE: returns a matrix of sampled sine wave
|
||||||
%phase shift is in number of periods
|
|
||||||
x = zeros(1, fs * duration);
|
% CONTRIBUTORS:
|
||||||
A = amplitude;
|
% Arthur Lu: Original author
|
||||||
f = frequency;
|
% Benjamin Liou: refactoring and annotations
|
||||||
p = phase;
|
|
||||||
n = fs * duration;
|
% DOCUMENTATION:
|
||||||
dt = 1 / fs;
|
% phase shift is in number of periods
|
||||||
|
% fs is the sampling frequency: how many sample points per second
|
||||||
|
% duration is time in seconds
|
||||||
|
% duty does not apply for sinusoids
|
||||||
|
|
||||||
|
|
||||||
|
% initialize local variables from input arguments
|
||||||
|
n = fs * duration; % number of samples (length of matrix)
|
||||||
|
dt = 1 / fs; % sampling period: time between two sample points
|
||||||
|
|
||||||
|
% initialize a one dimensional zero matrix to be populated
|
||||||
|
x = zeros(1, n);
|
||||||
|
|
||||||
|
% populate the matrix
|
||||||
for i = 1:n
|
for i = 1:n
|
||||||
t = i * dt;
|
t = i * dt; % time at the i'th sample
|
||||||
x(i) = A * sin(2 * pi * f * t - p);
|
x(i) = amplitude * sin(2 * pi * frequency * t - phase);
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -1,19 +1,40 @@
|
|||||||
function x = generate_square(amplitude, frequency, phase, fs, duration, duty)
|
function x = generate_square(amplitude, frequency, phase, fs, duration, duty)
|
||||||
%GENERATE_SINE:Arthur Lu returns a matrix of sampled sine wave, where the
|
% GENERATE_SQUARE: returns a matrix of sampled square wave
|
||||||
%phase shift is in number of periods
|
|
||||||
x = zeros(1, fs * duration);
|
% CONTRIBUTORS:
|
||||||
A = amplitude;
|
% Arthur Lu: Original author
|
||||||
f = frequency;
|
% Benjamin Liou: refactoring and annotations
|
||||||
p = phase;
|
|
||||||
n = fs * duration;
|
% DOCUMENTATION:
|
||||||
dt = 1 / fs;
|
% phase shift is in number of periods
|
||||||
|
% fs is the sampling frequency: how many sample points per second
|
||||||
|
% duration is time in seconds
|
||||||
|
% duty cycle should be a number between 0 and 1.
|
||||||
|
% duty of 0 or less would return -amplitude for all sample points
|
||||||
|
% duty of 0.25 would return +amplitude for first quarter of each cycle
|
||||||
|
% then return -amplitude for the remaining three-fourths
|
||||||
|
% duty of 1 would return all +amplitude
|
||||||
|
|
||||||
|
|
||||||
|
% initialize local variables from input arguments
|
||||||
|
n = fs * duration; % number of samples (length of matrix)
|
||||||
|
dt = 1 / fs; % sampling period: time between two sample points
|
||||||
|
|
||||||
|
% initialize a one dimensional zero matrix to be populated
|
||||||
|
x = zeros(1, n);
|
||||||
|
|
||||||
|
% populate the matrix
|
||||||
for i = 1:n
|
for i = 1:n
|
||||||
t = i * dt;
|
t = i * dt; % time at the i'th sample
|
||||||
st = mod(f * t - p, 1);
|
|
||||||
|
% periodic ramp from 0 to 1
|
||||||
|
% progression through a cycle
|
||||||
|
st = mod(frequency * t - phase, 1);
|
||||||
|
|
||||||
if(st < duty)
|
if(st < duty)
|
||||||
x(i) = A;
|
x(i) = amplitude;
|
||||||
else
|
else
|
||||||
x(i) = -A;
|
x(i) = -amplitude;
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -1,19 +1,43 @@
|
|||||||
function x = generate_triangle(amplitude, frequency, phase, fs, duration, duty)
|
function x = generate_triangle(amplitude, frequency, phase, fs, duration, duty)
|
||||||
%GENERATE_SINE:Arthur Lu returns a matrix of sampled sine wave, where the
|
% GENERATE_TRIANGLE: returns a matrix of sampled square wave
|
||||||
%phase shift is in number of periods
|
|
||||||
x = zeros(1, fs * duration);
|
% CONTRIBUTORS:
|
||||||
A = amplitude;
|
% Arthur Lu: Original author
|
||||||
f = frequency;
|
% Benjamin Liou: refactoring and annotations
|
||||||
p = phase;
|
|
||||||
n = fs * duration;
|
% DOCUMENTATION:
|
||||||
dt = 1 / fs;
|
% phase shift is in number of periods
|
||||||
|
% fs is the sampling frequency: how many sample points per second
|
||||||
|
% duration is time in seconds
|
||||||
|
% duty cycle should be a number between 0 and 1.
|
||||||
|
% duty of 0.25 would have positive slope for first quarter of each cycle
|
||||||
|
% then have negative slope for the remaining three-fourths
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
% initialize local variables from input arguments
|
||||||
|
n = fs * duration; % number of samples (length of matrix)
|
||||||
|
dt = 1 / fs; % sampling period: time between two sample points
|
||||||
|
|
||||||
|
% initialize a one dimensional zero matrix to be populated
|
||||||
|
x = zeros(1, n);
|
||||||
|
|
||||||
|
% populate the matrix
|
||||||
for i = 1:n
|
for i = 1:n
|
||||||
t = i * dt;
|
t = i * dt;
|
||||||
st = mod(f * t - p, 1);
|
|
||||||
|
% periodic ramp from 0 to 1
|
||||||
|
% progression through a cycle
|
||||||
|
st = mod(frequency * t - phase, 1);
|
||||||
|
|
||||||
if(st < duty)
|
if(st < duty)
|
||||||
x(i) = A*(1/duty * st - 0.5);
|
slope = amplitude / duty;
|
||||||
|
intercept = -0.5 * amplitude;
|
||||||
|
x(i) = slope * st + intercept;
|
||||||
else
|
else
|
||||||
x(i) = A*(-(1/(1-duty))*st + (duty/(1-duty)) + 1 - 0.5);
|
slope = -amplitude / (1 - duty);
|
||||||
|
intercept = amplitude*( duty/(1-duty) + 0.5);
|
||||||
|
x(i) = slope * st + intercept;
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
Reference in New Issue
Block a user