Matlab创建钢琴声音(奇怪的声音问题) [英] Matlab creating piano sounds (strange sound problem)

查看:880
本文介绍了Matlab创建钢琴声音(奇怪的声音问题)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,我必须构建一个程序,该程序能够播放贝多芬·艾莉斯的一些钢琴音

Hello I have to build a program that is able to play some piano sounds from beethoven elise

现在,我已经实现了频率和声音的功能,但是听起来很奇怪,一点也不像钢琴音符.

Now I have implemented the function for the frequency and the sound but it sounds strange and not at all like piano notes.

有人可以给我建议如何解决这个问题吗?

Could someone give me advice on how to fix this?

createWaveform(440,8000,2,1);


    function [sinusoid] = createWaveform(frequency,fs,duration,A)
     n = linspace(0,duration-1/fs,duration*fs);
     sinusoid = 1:duration*fs;
     for i = 1:duration*fs
      sinusoid(i) = A*cos(2*pi*frequency*n(i)*(1/fs)+(2*pi).*rand(1,1));
     end
    end


 end

这是我尝试为正确的钢琴音调创建音调的部分

Here is the part where I am attempting to build tones to the right piano note

%function [tone] = note(keynum,relDuration,fullDuration,fs)
   %basetone = 440;
   % frequency = basetone * nthroot(2,12)^(keynum-49);
  %[tone]=createWaveform(frequency,fs,relDuration*fullDuration,1);
%end

我已经删除了所有旧代码,并写了以下代码来完成工作

I've deleted all my old code and wrote the following that did the job

   %sound(createWaveform(1000,8000,1,1));
   sound(note(24,1/2,1,8000));



   function [sinusoid] = createWaveform(frequency,fs,duration,A)
    Fs = fs;                   % samples per second
    dt = 1/Fs;                   % seconds per sample           % seconds
    t = (0:dt:duration)';     % seconds
    %%Sine wave:
    Fc = frequency;                     % hertz
    sinusoid = A.*cos(2*pi*Fc*t+(2*pi).*rand(1,1));
   % Plot the signal versus time:
   end

   function [tone] = note(keynum,relDuration,fullDuration,fs)
             basetone = 440;
             frequency = basetone * nthroot(2,12)^(keynum-49);
             tone=createWaveform(frequency,fs,relDuration*fullDuration,0.5);
   end

   function [E] = envel(relDuration,fullDuration,fs)
   param = 0;
   for i = 0:fs:relDuration*fullDuration
    E(i)=relDuration*fullDuration;
    i=i+1;
   end

   end

推荐答案

如注释中所述,钢琴发出的声音非常复杂.因此,这不是Matlab的挑战,只是一个数学问题.

As mentioned in the Comments, the sound that a piano makes is quite complicated. As such this is not a Matlab challenge, just a math problem.

现在您已经知道了这一点,您只需要提出一个合理的数学近似值,就可以从这里开始直接在Matlab中实现.

Now that you know this, you 'just' need to come up with a reasonable mathematical approximation, and from here it should be straight forward to implement in Matlab.

以下是一些让您开始数学的东西:

Here is something to get you started with the math:

https://dsp.stackexchange .com/questions/46598/钢琴制作的声波的数学方程式

这篇关于Matlab创建钢琴声音(奇怪的声音问题)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆