Reorganized Into Folders

This commit is contained in:
Darell Chua Yun Da
2021-12-10 10:01:30 +08:00
parent 88f77fed35
commit f856cdcd2b
41 changed files with 53 additions and 4 deletions

View File

@@ -0,0 +1,44 @@
function x = generate_halfCircles(amplitude, frequency, phase, fs, duration, duty)
% Generates half circles.
% By Conner Hsu
% DOCUMENTATION:
% amplitude scales how tall the half circle is.
% 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 0 for all sample points
% duty of 0.25 would return a half circle for first quarter of each cycle
% then return 0 for the remaining 3/4ths
% 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
t = i * dt; % time at the i'th sample
% periodic ramp from 0 to 1
% progression through a cycle
st = mod(frequency * t - phase, 1);
if(st < duty)
x(i) = sqrt((duty/2)^2-(st-duty/2)^2)/2*amplitude;
else
x(i) = 0;
end
end
%Testing code.
%t = 0:dt:duration;
%t(n) = [];
%plot(t, x);
sound(x, fs);
end

View File

@@ -0,0 +1,40 @@
function x = generate_sawtooth(amplitude, frequency, phase, fs, duration, duty)
% generate_sawtooth: returns a matrix of sampled sawtooth wave
% CONTRIBUTORS:
% Ben Zhang: Function author
% DOCUMENTATION:
% phase shift is in number of periods
% fs is the sampling frequency: how many sample points per second
% duration is time in seconds
% duty is a number between 0 and 1
%duty does not apply for sawtooth wave
% 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
period = 1 / frequency; % period of the wave
% initialize a one dimensional zero matrix to be populated
x = zeros(1, n);
% populate the matrix
for i = 1:n
t = i * dt; % time of the i'th sample
st = mod(frequency * t - phase, 1); % Progression through cycle
slope = 2 * amplitude / period; % the incline slope from start to amplitude
mid = period / 2;
%part before the straght vertical line
if(st < mid)
x(i) = slope * st; % amplitude from start to +amplitude
%part after the straght vertical line
else
x(i) = slope * (st - 0.5) - amplitude; %amplitude from -amplitude to start
end
end

View File

@@ -0,0 +1,28 @@
function x = generate_sine(amplitude, frequency, phase, fs, duration, duty)
% GENERATE_SINE: returns a matrix of sampled sine wave
% CONTRIBUTORS:
% Arthur Lu: Original author
% Benjamin Liou: refactoring and annotations
% DOCUMENTATION:
% 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
t = i * dt; % time at the i'th sample
x(i) = amplitude * sin(2 * pi * frequency * t - phase);
end
end

View File

@@ -0,0 +1,41 @@
function x = generate_square(amplitude, frequency, phase, fs, duration, duty)
% GENERATE_SQUARE: returns a matrix of sampled square wave
% CONTRIBUTORS:
% Arthur Lu: Original author
% Benjamin Liou: refactoring and annotations
% DOCUMENTATION:
% 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
t = i * dt; % time at the i'th sample
% periodic ramp from 0 to 1
% progression through a cycle
st = mod(frequency * t - phase, 1);
if(st < duty)
x(i) = amplitude;
else
x(i) = -amplitude;
end
end
end

View File

@@ -0,0 +1,43 @@
function x = generate_triangle(amplitude, frequency, phase, fs, duration, duty)
% GENERATE_TRIANGLE: returns a matrix of sampled square wave
% CONTRIBUTORS:
% Arthur Lu: Original author
% Benjamin Liou: refactoring and annotations
% DOCUMENTATION:
% 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
t = i * dt;
% periodic ramp from 0 to 1
% progression through a cycle
st = mod(frequency * t - phase, 1);
if(st < duty)
slope = amplitude / duty;
intercept = -0.5 * amplitude;
x(i) = slope * st + intercept;
else
slope = -amplitude / (1 - duty);
intercept = amplitude*( duty/(1-duty) + 0.5);
x(i) = slope * st + intercept;
end
end
end

View File

@@ -0,0 +1,13 @@
function x = generate_white(amplitude, fs, duration)
%GENERATE_WHITE: returns a matrix of sampled white noise
%CONTRIBUTORS:
%Benjamin Liou: Original author
%DOCUMENTATION:
% white noise can then be filtered to produce different hues of noises
%time domain matrix of random sample points between +-amplitude
x = amplitude * 2 * (rand(1, fs * duration) - 0.5);
end