MATLAB: Multiply sound by pulse

audiomatrixpulsepulse trainsound

How do I make a pulse and make the time of the pulse match the audio file time? For the pulse I would like the period to be 2 seconds, and amp of 1, and a duty cycle of 50%. I would then like to multiply the pulse by the audio file.

Best Answer

Is your signal stereo instead of monochannel? You need to take that into account.
See this little demo for mono, and adapt as needed:
% Initialization steps.
clc; % Clear the command window.
fprintf('Beginning to run %s.m ...\n', mfilename);
close all; % Close all figures (except those of imtool.)
clear; % Erase all existing variables. Or clearvars if you want.
workspace; % Make sure the workspace panel is showing.
format short g;
format compact;
fontSize = 15;
% Read in the sound.
[y, Fs] = audioread('guitartune.wav');
% load('handel.mat'); % Another sound you can load.
% Play the sound.

soundsc(y, Fs);
% Compute the time axis
t = (1 : length(y)) / Fs;
subplot(3, 1, 1);
plot(t, y, 'b-', 'LineWidth', 2);
title('Original Audio Waveform', 'FontSize', fontSize);
xlabel('Seconds', 'FontSize', fontSize);
ylabel('Amplitude', 'FontSize', fontSize);
xticks(1 : round(t(end)));
grid on;
drawnow;
audioTime = length(y) / Fs; % In seconds.
fprintf('Audio length = %.3f seconds.\n', audioTime);
% Compute how many seconds 2 seconds is.
pulseWidth = Fs; % Whatever it needs to get 1 (half of 2) of whatever units you're using.
onePulse = [ones(1, pulseWidth), zeros(1, pulseWidth)];
% Replicate as many times as we need (more actually)
numReps = ceil(audioTime/2);
pulseTrain = repmat(onePulse, [1, numReps]);
% Now crop down to the actual signal length that y is
pulseTrain = pulseTrain(1 : length(y));
% Reshape to be a column vector like y
pulseTrain = pulseTrain(:);
% Plot the pulse train.

subplot(3, 1, 2);
plot(t, pulseTrain, 'b-', 'LineWidth', 2);
title('Pulse Train', 'FontSize', fontSize);
xlabel('Seconds', 'FontSize', fontSize);
ylabel('Amplitude', 'FontSize', fontSize);
grid on;
xticks(1 : round(t(end)));
% Multiple the pulse train by the audio signal and plot.
yPulsed = y .* pulseTrain;
% Plot the pulse train.
subplot(3, 1, 3);
plot(t, yPulsed, 'b-', 'LineWidth', 2);
title('Pulsed Waveform', 'FontSize', fontSize);
xlabel('Seconds', 'FontSize', fontSize);
ylabel('Amplitude', 'FontSize', fontSize);
grid on;
xticks(1 : round(t(end)));
pause(audioTime);
% Play the sound.
soundsc(yPulsed, Fs);
fprintf('Done running %s.m ...\n', mfilename);