# Mixing an audio wav file with a generated sin wave sound

조회 수: 12(최근 30일)
Muhammad Talha Bashir 2022년 6월 11일
편집: Jan 2022년 6월 11일
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표시숨기기 이전 댓글 수: 1
Jeffrey Clark 2022년 6월 11일
@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.

댓글을 달려면 로그인하십시오.

### 답변(2개)

Image Analyst 2022년 6월 11일
Try this:
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')
outputSound = y + monoTone;
subplot(3, 1, 3);
plot(timeValues, outputSound, 'g-');
grid on;
title('The Sum')
xlabel('Time')
ylabel('Signal')
##### 댓글 수: 0표시숨기기 이전 댓글 수: -1

댓글을 달려면 로그인하십시오.

Jan 2022년 6월 11일
편집: Jan 2022년 6월 11일
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?

댓글을 달려면 로그인하십시오.

R2015a

### Community Treasure Hunt

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

Start Hunting!

Translated by