diff --git a/src/generate_sine.m b/src/generate_sine.m new file mode 100644 index 0000000..76c4a09 --- /dev/null +++ b/src/generate_sine.m @@ -0,0 +1,15 @@ +function x = generate_sine(amplitude, frequency, phase, fs, duration) +%GENERATE_SINE:Arthur Lu returns a matrix of sampled sine wave, where the +%phase shift is in number of periods + x = zeros(1, fs * duration); + A = amplitude; + f = frequency; + p = phase; + n = fs * duration; + dt = 1 / fs; + for i = 1:n + t = i * dt; + x(i) = A * sin(2 * pi * f * t - p); + end +end + diff --git a/src/generate_square.m b/src/generate_square.m new file mode 100644 index 0000000..bf3134e --- /dev/null +++ b/src/generate_square.m @@ -0,0 +1,20 @@ +function x = generate_square(amplitude, frequency, phase, fs, duration, duty) +%GENERATE_SINE:Arthur Lu returns a matrix of sampled sine wave, where the +%phase shift is in number of periods + x = zeros(1, fs * duration); + A = amplitude; + f = frequency; + p = phase; + n = fs * duration; + dt = 1 / fs; + for i = 1:n + t = i * dt; + st = mod(f * t - p, 1); + if(st < duty) + x(i) = A; + else + x(i) = -A; + end + end +end + diff --git a/src/generate_triangle.m b/src/generate_triangle.m new file mode 100644 index 0000000..94b78ad --- /dev/null +++ b/src/generate_triangle.m @@ -0,0 +1,19 @@ +function x = generate_triangle(amplitude, frequency, phase, fs, duration, duty) +%GENERATE_SINE:Arthur Lu returns a matrix of sampled sine wave, where the +%phase shift is in number of periods + x = zeros(1, fs * duration); + A = amplitude; + f = frequency; + p = phase; + n = fs * duration; + dt = 1 / fs; + for i = 1:n + t = i * dt; + st = mod(f * t - p, 1); + if(st < duty) + x(i) = A*(1/duty * st - 0.5); + else + x(i) = A*(-(1/(1-duty))*st + (duty/(1-duty)) + 1 - 0.5); + end + end +end \ No newline at end of file diff --git a/src/play_wave.m b/src/play_wave.m new file mode 100644 index 0000000..56d4c26 --- /dev/null +++ b/src/play_wave.m @@ -0,0 +1,5 @@ +function play_wave(wave, fs) +%PLAY_WAVE:Arthur Lu plays a wave as sound + sound(wave, fs) +end + diff --git a/src/plot_wave.m b/src/plot_wave.m new file mode 100644 index 0000000..b28a588 --- /dev/null +++ b/src/plot_wave.m @@ -0,0 +1,8 @@ +function plot_wave(waveform, fs, duration) +%PLOT_WAVE:Arthur Lu plots a waveform + x = linspace(0, duration, fs * duration); + plot(x, waveform); + xlabel("time"); + ylabel("amplitude"); +end +