From cb6547ad0bb664a631e8379e0f5ceb8a3b35fd37 Mon Sep 17 00:00:00 2001 From: Daniel Doan Date: Fri, 10 Dec 2021 01:50:47 -0800 Subject: [PATCH 1/4] created generate_trapezoid.m --- src/generate_trapezoid.m | 55 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 src/generate_trapezoid.m diff --git a/src/generate_trapezoid.m b/src/generate_trapezoid.m new file mode 100644 index 0000000..69ce591 --- /dev/null +++ b/src/generate_trapezoid.m @@ -0,0 +1,55 @@ +function x = generate_trapezoid(amplitude, frequency, phase, fs, duration, duty) +% GENERATE_TRAPEZOID: returns a matrix of sampled square wave + +% CONTRIBUTORS: +% Daniel Doan: 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 cycle should be a number between 0 and 1. + % duty of 0.5 would have 2 trapezoids in first half of each cycle + % example of wave with duty of 0.5, where the peaks are amplitude/2: + % + % ____ + % / \ + % / \ ________________ + % \ / + % \____/ + % + + + +% 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); + slope = (amplitude/2) / (duty/8); + if(st < duty) + if(st < duty/8 || st > 7*duty/8) + x(i) = slope * st; + else + if(st < 5*duty/8) + x(i) = amplitude/2 - slope * (st-(3*duty/8)); + end + if(st < 3*duty/8) + x(i) = amplitude/2; + end + if(st > 5*duty/8) + x(i) = -amplitude/2; + end + end + end + end +end \ No newline at end of file From 23fb666a57722d7993767baefbe93049c48d8e22 Mon Sep 17 00:00:00 2001 From: SeaSponge <61109988+Squidwarder@users.noreply.github.com> Date: Fri, 10 Dec 2021 08:59:33 -0800 Subject: [PATCH 2/4] fixed fade_in.m changed it so that it accepts instances of time where t < 1 and not a whole number --- src/NotWorking/fade_in.m | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/src/NotWorking/fade_in.m b/src/NotWorking/fade_in.m index 254d288..bebb07f 100644 --- a/src/NotWorking/fade_in.m +++ b/src/NotWorking/fade_in.m @@ -12,10 +12,21 @@ function output = fade_in(input, time) len = length(input); + % need to use whole number for time + time = round(time); + % if time parameter longer than signal, treat time as % the duration of original signal if time > len - time = len + time = len; + end + + % in order to create array, time >=1 + % if not, it's arbitrarily set to 1 + % in which case the fade_in effect + % is virtually nonexistent + if time < 1 + time = 1; end % set multiplier as 1D array From 48a6dfa8ad4ccf0be3e73ecf8ed2f8a02e3b2844 Mon Sep 17 00:00:00 2001 From: SeaSponge <61109988+Squidwarder@users.noreply.github.com> Date: Fri, 10 Dec 2021 09:07:09 -0800 Subject: [PATCH 3/4] Fixed fade_out.m and moved it to "Helper" folder Made it so that it now accepts instances of time where: t < 1 or t is not a whole number. Also moved it to the 'Helper' folder --- src/{NotWorking => Helper}/fade_out.m | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) rename src/{NotWorking => Helper}/fade_out.m (77%) diff --git a/src/NotWorking/fade_out.m b/src/Helper/fade_out.m similarity index 77% rename from src/NotWorking/fade_out.m rename to src/Helper/fade_out.m index f0821d3..954a1bb 100644 --- a/src/NotWorking/fade_out.m +++ b/src/Helper/fade_out.m @@ -11,18 +11,29 @@ function output = fade_out(input, time) len = length(input); + % need to use time as a whole number + time = round(time); + % if time parameter longer than signal, treat time as % the duration of original signal if time > len time = len end + % in order to create array, time >=1 + % if not, it's arbitrarily set to 1 + % in which case the fade_in effect + % is virtually nonexistent + if time < 1 + time = 1; + end + % set multiplier as 1D array multiplier = (1 : time) / time; % fade out effect: from full volume of signal to no volume - multiplier = flip(multiplier) + multiplier = flip(multiplier); while length(multiplier) < len multiplier = [multiplier 0]; From 2b073610e0796c1c9ba2a5f764e07da6729d731b Mon Sep 17 00:00:00 2001 From: SeaSponge <61109988+Squidwarder@users.noreply.github.com> Date: Fri, 10 Dec 2021 09:09:49 -0800 Subject: [PATCH 4/4] Fixed fade_in.m and moved it to "Helper" folder Made it so that it now accepts instances of time where: t < 1 or t is not a whole number. Also moved it to the 'Helper' folder --- src/{NotWorking => Helper}/fade_in.m | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename src/{NotWorking => Helper}/fade_in.m (100%) diff --git a/src/NotWorking/fade_in.m b/src/Helper/fade_in.m similarity index 100% rename from src/NotWorking/fade_in.m rename to src/Helper/fade_in.m