How to generate sound in Matlab?
64 ビュー (過去 30 日間)
古いコメントを表示
I want to generate sound for my sine wave but the sound did not come out. Is there something wrong with my code? Really need help, thanks.
fsampling = 1000;
f1 = 20;
f2 = 100;
f3 = 300;
fn1 = 150;
fn2 = 200;
fn1_normfreq = fn1/(fsampling/2);
fn2_normfreq = fn2/(fsampling/2);
x1 = cos(2*pi*f1*[0:1/fsampling:1.23]);
x2 = cos(2*pi*f2*[0:1/fsampling:1.23]);
x3 = cos(2*pi*f3*[0:1/fsampling:1.23]);
x = x1 + x2 + x3;
x(end) = [];
[b,a] = butter(2,[fn1_normfreq fn2_normfreq],'bandpass');
filtered_noise = filter(b,a,randn(1, length(x)*2));
y = (x + 0.5*filtered_noise(500:500+length(x)-1))/length(x)*2;
sound(y,fsampling)
0 件のコメント
採用された回答
Chad Greene
2015 年 11 月 17 日
Use
soundsc(y,fsampling)
which scales the sound before playing it.
3 件のコメント
Chad Greene
2015 年 11 月 17 日
You could repeat y as many times as you want. This plays y five times in a row:
y_five_times = repmat(y,1,5);
soundsc(y_five_times,fsampling)
To get y to play for a specified amount of time, you'll have to repeat y enough times to fill the desired length, then only play the number of samples necessary to meet the time requirement:
% Enter how long you want the signal to be in seconds:
Desired_length = 7.5;
% Number of samples necessary to make the sound the desired length:
num_samples = round(Desired_length*fsampling);
% Number of times y must be repeated:
num_repeats = ceil(num_samples/length(y));
% Create a vector of y repeated as many times as necessary:
y_long = repmat(y,1,num_repeats);
% Clip y_long to be exactly the desired length:
y_long_clipped = y_long(1:num_samples);
% Play the sound:
soundsc(y_long_clipped,fsampling)
その他の回答 (1 件)
Image Analyst
2015 年 11 月 17 日
For what it's worth, I have attached a demo that creates a weird sound and plays it.
0 件のコメント
参考
カテゴリ
Help Center および File Exchange で Audio I/O and Waveform Generation についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!