Skip to content

Project Wavephile [Version: α.0.1]

Creating Music Beat Using Pure MATLAB Code and Wave Functions

MATLAB Live Script

Code:

clear 
close 
clc 

% Variables for Wave Function:
magnitude = 1;
phase_shift = 0;
vertical_shift = 0;
ct_frequency_f = [4e2 3e2 2e2 3e2 1e2 1e2];

% Total Duration of the Resultant Wave:
constant = 1e2; % Nyquist's Theorem 
Fs = constant*max(ct_frequency_f); % Sampling Frequency
second = 10; 

% Types of Waves and Modifications:
type_wave = 1; % 0 for sine; 1 for triangle
loop_on = 0; % 0 for alternating and spacing; 1 for mixing

% Other:
power = [1 1 1 1 1 1]; % Increment speed of the wave frequency
window_frequency = 0.1; 
number_space_wave = 0; % Number of space between waves
plot_zoom = 100;
sound_on = 1; 

% Wave in Time Domain and Frequency Domain:
[wave_result, t] = mix_wave(magnitude, phase_shift, vertical_shift, ct_frequency_f, second, Fs, power, type_wave, loop_on, window_frequency, number_space_wave, plot_zoom);

% Sound Enable/Disable
if sound_on == 1
    soundsc(wave_result, Fs)
end

Function: mix_wave

function [wave_result, t] = mix_wave(magnitude, phase_shift, vertical_shift, ct_frequency_f, second, Fs, power, type_wave, loop_on, window_frequency, number_space_wave, plot_zoom)
    % Date: 02/28/2022 - 03/03/2022
    % By: Ka Ming Lui
    
    % Variables calculations
    count_wave_type = length(ct_frequency_f);
    sample_wave = second*Fs;
    number_window = second*window_frequency;
    
    % Initializing arrays
    wave_function = zeros(count_wave_type, second*Fs);
    X = zeros(count_wave_type, max([sample_wave,2^20])); % default minimum assign of myFFT
    magnitude_X = X;
    
    % Generating wave-type according to the inputs
    if type_wave == 0 % Sine wave
        for loop_count_1 = 1:count_wave_type
            [wave_function(loop_count_1, :), t] = sine_wave(magnitude, phase_shift, vertical_shift, ct_frequency_f(loop_count_1), second, Fs, power(loop_count_1));
        end
    elseif type_wave == 1 % Triangle wave
        for loop_count_1 = 1:count_wave_type
            [wave_function(loop_count_1, :), t] = triangle_wave(magnitude, phase_shift, vertical_shift, ct_frequency_f(loop_count_1), second, Fs);
        end
    end
    
    % Modifying the placement method of the generated waves
    if loop_on == 0 % Alternating and adding space between waves
        normal_wave_window = count_wave_type*number_window;
        total_wave_window = normal_wave_window*(number_space_wave + 1);
        length_window = floor(sample_wave/total_wave_window);

        count_window = 1;
        count_wave_type_loop = 1;
        for loop_count_2 = 1:normal_wave_window % Adding wave
            wave_result(count_window:count_window + length_window - 1) = wave_function(count_wave_type_loop, count_window:count_window + length_window - 1);
            count_window = count_window + length_window;
            for loop_count_3 = 1:number_space_wave % Adding space
                wave_result(count_window:count_window + length_window - 1) = 0;
                count_window = count_window + length_window;
            end
            count_wave_type_loop = count_wave_type_loop + 1;
            if count_wave_type_loop == count_wave_type
                count_wave_type_loop = 1;
            end
        end
    else % Mixing waves with different frequencies into one
        wave_result = zeros(1, second*Fs);
        for loop_count_2 = 1:count_wave_type
            wave_result = wave_result + wave_function(loop_count_2, :);
        end
    end
    
    % Unify the size of the arrays
    wave_result = [wave_result zeros(1, length(t) - length(wave_result))];
    
    % Generating plots for each waves in different frequencies
    % Credit: myFFT - Dr. Iyad Obeid
    count_plot = 1;
    for loop_count_4 = 1:count_wave_type
        [X(loop_count_4, :), f] = myFFT(wave_function(loop_count_4, :), Fs); 
        magnitude_X(loop_count_4, :) = abs(X(loop_count_4, :)); 
        subplot(count_wave_type + 1, 2, count_plot); plot(t(1:length(t)/plot_zoom), wave_function(loop_count_4, 1:length(t)/plot_zoom)); title('Time Domain');
        count_plot = count_plot + 1;
        subplot(count_wave_type + 1, 2, count_plot); plot(f, magnitude_X(loop_count_4, :)); xlim([0 max(ct_frequency_f)*2]); grid on; title('Frequency Domain');
        count_plot = count_plot + 1;
    end
    
    % Generating plots for the result wave
    [X_result, f] = myFFT(wave_result, Fs); 
    magnitude_X_result = abs(X_result); 
    subplot(count_wave_type + 1, 2, count_plot); plot(t, wave_result); title('Time Domain, Resultant');
    count_plot = count_plot + 1;
    subplot(count_wave_type + 1, 2, count_plot); plot(f, magnitude_X_result); xlim([0 max(ct_frequency_f)*2]); grid on; title('Frequency Domain, Resultant');
end

Function: triangle_wave

function [wave_function, t] = triangle_wave(magnitude, phase_shift, vertical_shift, ct_frequency_f, second, Fs)
    tick = second*Fs; 
    t = linspace(0, second, tick);
    wave_function = magnitude*(2/pi)*asin(sin(2*pi*ct_frequency_f.*t + phase_shift)) + vertical_shift;
end

Function: sine_wave

function [wave_function, t] = sine_wave(magnitude, phase_shift, vertical_shift, ct_frequency_f, second, Fs, power)
    tick = second*Fs; 
    t = linspace(0, second, tick);
    wave_function = magnitude*sin(2*pi*ct_frequency_f.*t.^power + phase_shift) + vertical_shift;
end

Published inMATLAB

Be First to Comment

Leave a Reply