MATLAB Answers

Creating a sound with certain obtained frequencies

8 views (last 30 days)
Heeje Lee
Heeje Lee on 22 Jul 2021
Edited: Les Beckham on 22 Jul 2021
I am trying to create a new sound with certain frequencies I found using the pitch function. I gathered the frequencies into one vector (f0). I based my code on this particular question: https://www.mathworks.com/matlabcentral/answers/519684-generate-sound-with-specific-frequencies but every time I run the code, it says that there is an error using sound (Only one and two channel audio supported). Is there anyway I could fix this? Also, if there is any other way I could produce a sound with specific frequencies, please let me know too. I am currently aware of the chirp function, but also having a difficult time with that too. Thank you!
amp = 0.5;
Fs = sum(f0, 'all');
Ts = 1/Fs;
T = 0:Ts:1; %play each frequncy for 1 second
x = [];
for freq = f0
x = [ x amp * sin(2*pi*freq*T)];
end
sound(x,Fs)

Answers (1)

Les Beckham
Les Beckham on 22 Jul 2021
Edited: Les Beckham on 22 Jul 2021
Try this. Since you didn't provide f0 in your example code, I just picked some frequencies. Also, I changed to a fixed output frequency. I'm not sure what you meant to do with the calculation of the sampling frequency as a sum of the output frequencies.
% answers_88349.m
%
% https://www.mathworks.com/matlabcentral/answers/883449-creating-a-sound-with-certain-obtained-frequencies?s_tid=srchtitle
f0 = [4000 6000 8000]; % not provided in question code
amp = 0.5;
% Fs = sum(f0, 'all');
Fs = 41000; % Better (I think) to use a fixed frequency.
Ts = 1/Fs;
T = 0:Ts:1; %play each frequency for 1 second
x = [];
for freq = f0
x = [ x, amp * sin(2*pi*freq*T)];
end
sound(x,Fs)
I'm not sure how this addresses the error you say you are getting, but this works for me. Since you didn't provide a full (not working) example, or provide the full text of your error message, it is difficult to know more about what was going on.
However, this code works for me.

Products


Release

R2021a

Community Treasure Hunt

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

Start Hunting!