Mixing an audio wav file with a generated sin wave sound

11 ビュー (過去 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 Center および File ExchangeCode Generation and Deployment についてさらに検索

タグ

製品


リリース

R2015a

Community Treasure Hunt

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

Start Hunting!

Translated by