Mixing an audio wav file with a generated sin wave sound

11 views (last 30 days)
I am trying to add a aound signal "a" with a .wav format named "song".
amp=1;
fs=20500; % sampling frequency
duration=2;
freq=8000;
values=0:1/fs:duration;
a=amp*sin(2*pi*freq*values);
the problem is to mix this "a" with song and save as a .wav file.
  2 Comments
Image Analyst
Image Analyst on 11 Jun 2022
The code in the error message in your screenshot does not match the code in the body of your message. You can fix that after reading this:
Jeffrey Clark
Jeffrey Clark on 11 Jun 2022
@Muhammad Talha Bashir as MATLAB tells you, song and a must be exactly the same size. In your case 1443108x2 is not the same as 1x8401. You need to match the wav's sample rate, duration and number of channels when creating your tone signal.

Sign in to comment.

Answers (2)

Image Analyst
Image Analyst on 11 Jun 2022
Try this:
[y, fs] = audioread('guitartune.wav');
timeValues = (1 : length(y)) / fs;
subplot(3, 1, 1);
plot(timeValues, y, 'b-');
grid on;
xlabel('Time')
ylabel('Signal')
title('Original Sound File')
amplitude = 1;
% fs = 20500; % sampling frequency
% duration = 2;
freq = 8000;
% timeValues = 0 : (1 / fs) : duration;
monoTone = amplitude * sin(2 * pi * freq * timeValues);
monoTone = monoTone'; % Reshape into column vector.
subplot(3, 1, 2);
plot(timeValues, monoTone, 'r-');
grid on;
title('Mono-Tone')
xlabel('Time')
ylabel('Signal')
% Add together
outputSound = y + monoTone;
subplot(3, 1, 3);
plot(timeValues, outputSound, 'g-');
grid on;
title('The Sum')
xlabel('Time')
ylabel('Signal')

Jan
Jan on 11 Jun 2022
Edited: Jan on 11 Jun 2022
The variable song is a [1443108 x 2] matrix representing a stereo signal. The sine wave is a [1 x 8401] vector. You cannot add them, because this operation is not mathematically defined.
Let a have the same number of frames as the sound and create it as column vector. If you add a [N x 1] vector to a [N x 2] matrix, Matlab expands it over the 2nd dimension automatically.
linspace is useful to create a vector with a certain number of elements.
What is the desired output? Should the sine be added over the full range of the sound? What do you want to do with values outside the range [-1, +1]? Should they be cropped?

Categories

Find more on Code Generation and Deployment in Help Center and File Exchange

Tags

Products


Release

R2015a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!