Mixing an audio wav file with a generated sin wave sound

조회 수: 25 (최근 30일)
Muhammad Talha Bashir
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
Image Analyst
Image Analyst 2022년 6월 11일
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 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
Image Analyst 2022년 6월 11일
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 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?

카테고리

Help CenterFile Exchange에서 Multirate Signal Processing에 대해 자세히 알아보기

태그

제품


릴리스

R2015a

Community Treasure Hunt

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

Start Hunting!

Translated by