• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

Anybody help me with MATLAB ?

z1ggy

Lifer
I have a MATLAB project where I need to create a song in matlab using frequencies and using the "sound" command to play them as notes. The catch is They need to be outputted to sound like a piano when the normal is just something like a computer beep, but at different tones. Does anybody knows how to do this? I was given a hint that I need to multiply each note by a certain multiple of the fundamental frequency.
 
I could help you code it, but I think you need to understand a little more about how the piano works before you will be able to reproduce its sound this way. I could help you with that too, except I have no idea how it works. 😛
 
All instruments have harmonics. How they sound generally depends on how much of each harmonic is produced in the spectrum of the sound (e.g., a piano will emphasize lower harmonics while a violin will emphasize higher harmonics). You need to figure out what the spectrum of a piano note looks like and try to reproduce that in MATLAB (I'd start by importing an audio file with a single piano note in MATLAB and plotted the spectrum of it in MATLAB).
 
well I know that each note on the keyboard has a fundamental frequency of 440 Hz and then each note is raised to (x/12) where x depends on which note you are trying to play. I just don't understand how to change this so instead of that computer beeping tone I get a piano like ouput sound.
 
Have you learned about Fourier transforms? Do you understand what the Fourier transform of a signal is?
 
I got bored and curious tonight, so I worked on it for about half an hour. Here is the code I came up with, which gives more synthesizer-sounding music than piano, but not too bad. It plays the first few measures of "Twinkle, Twinkle, Little Star," approximately anyway. I don't know how to play the piano, but I recall the basic tune from my days as a violinist many moons ago. The keyfreqs.txt is simply a sorted list of piano key frequencies borrowed from here. I don't know anything about music theory, so I can't explain why the notes don't sound quite like a piano. Perhaps I need to include a higher number of overtones, but that just takes longer computationally and I'm not bored enough to wait around for that. 😛

Anyway, here's the sloppy code.

overtones=25;%number of overtones to include
duration=0.5;%seconds
SampleFreq=44100;%44.1 kHz sample rate
duration=[1,1,1,1,1,1,2,1,1,1,1,1,1,2].*duration;
keys=[49,49,56,56,58,58,56,55,55,53,53,51,51,49];
NumNotes=length(keys);

freqs=dlmread('keyfreqs.txt');

Instrument=0;

Song=[];
%Generate the sound waveform
for i=1:NumNotes
%Allow for variable note duration
NumPoints=duration(i)*SampleFreq;
t=linspace(0,duration(i),NumPoints);

% f=fbase*2^((i-a)/12);%Produce octaves for piano
key=keys(i);
y=MakeSound(t,freqs(key),overtones,Instrument);
Song=vertcat(Song,y');
end
soundsc(Song,SampleFreq);

function y=MakeSound(t,f,overtones,Instrument)

y=sin(2.*pi.*f.*t);
if overtones~=0
for i=1😱vertones
if Instrument==0
%Perfect harmonic overtones
y=y+sin(2.*pi.*f.*i.*t);
elseif Instrument==1
%odd multiples only
y=y+sin(2.*pi.*f.*(2.*i-1).*t);
elseif Instrument==2
%even multiples only
y=y+sin(2.*pi.*f.*2.*i.*t);
end
end
end
 
To make the sound more natural, add the "attack-decay-sustain-release" envelope to make the final note. You can just model each step as a piece-wise function. It's a lot easier this way - nothing fancy. The function linspace() is very useful, so it might be good to know how to use it well.

You have to sample your note at greater than the nyquist frequency, otherwise, you might get aliasing.

http://en.wikipedia.org/wiki/ADSR_envelope
http://en.wikipedia.org/wiki/Nyquist_frequency
http://en.wikipedia.org/wiki/Aliasing
 
Back
Top