matlab 코딩해석점요..

soniya의 이미지


1. bestSound(초간단)

%%%%%%%%%%%% 여기서부터 %%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function [ madeSound ] = beatSound( tone, oct, beat )
%UNTITLED1 Summary of this function goes here
% Detailed explanation goes here

switch tone
case 'C'
freqSound = 264*2^((12*(oct-1)+0)/12);
case 'Db'
freqSound = 264*2^((12*(oct-1)+1)/12);
case 'D'
freqSound = 264*2^((12*(oct-1)+2)/12);
case 'Eb'
freqSound = 264*2^((12*(oct-1)+3)/12);
case 'E'
freqSound = 264*2^((12*(oct-1)+4)/12);
case 'F'
freqSound = 264*2^((12*(oct-1)+5)/12);
case 'Gb'
freqSound = 264*2^((12*(oct-1)+6)/12);
case 'G'
freqSound = 264*2^((12*(oct-1)+7)/12);
case 'Ab'
freqSound = 264*2^((12*(oct-1)+8)/12);
case 'A'
freqSound = 264*2^((12*(oct-1)+9)/12);
case 'Bb'
freqSound = 264*2^((12*(oct-1)+10)/12);
case 'B'
freqSound = 264*2^((12*(oct-1)+11)/12);
case 'R'
freqSound = 264*2^((12*(oct-1)-1)/12);
end

Fs = 44100; %Fs:샘플링주파수,
T = beat; % 지속시간. 몇분음표인지에 따라 다르다.
n = [0:(T*Fs)-1]';

%---------------------------- 음 생성
madeSound = sin(2*pi*freqSound*n/Fs);

%---------------------------- 자연스러운 음 감쇄적용
toneL = length(madeSound);
t = linspace(0,3,toneL)';
expFunc = exp(-t);
madeSound = madeSound.*expFunc;
%----------------------------
%----------------------------음 재생
wavplay(madeSound,Fs,'sync');

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% 아래와 같이 실행하면 음 재생.
Tempo = 120; % 템포 맞추기
%----------------------------
%----------------------------
beat1 = 60/Tempo*4;
beat2 = 60/Tempo*2;
beat4 = 60/Tempo; % 기본 4분음표
beat8 = 60/Tempo/2;
beat16 = 60/Tempo/4;
beat32 = 60/Tempo/8;
%----------------------------
%----------------------------
a(:,1) = beatSound('C',1,beat8); %1은 옥타브를 뜻함.
a(:,2) = beatSound('D',1,beat8);
a(:,3) = beatSound('E',1,beat8);
a(:,4) = beatSound('F',1,beat8);
a(:,5) = beatSound('G',1,beat8);
a(:,6) = beatSound('A',1,beat8);
a(:,7) = beatSound('B',1,beat8);
a(:,8) = beatSound('C',2,beat8);
%----------------------------
%%%%%%%%%%%%% 여기까지 %%%%%%%%%%%%%%%

첫번째로.. freqsound 를 어떻게 만들어야 하는지가 궁금하고요.. 음악적인 지식이 필요한가요?

그담에.. 그렇다면 어떻게 freqsound 가 만들어 지는지 알려주세요.. ㅠ

그담에 위에 글에서.. 자연스러운 음 감쇄작용

이것도 어떻게 되는 원리 인지 설명점요... ㅠ

부탁 드려요..

남에게 설명할 수 있도록 해석하는것좀 도와주세요 ㅠ

감사합닏~ 조은하루 되세요~

seoleda의 이미지

1. wavplay 까지 beatSound 함수의 끝입니다.

다음코드를 main.m으로 저장해서 실행해 보세요.

function main
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% 아래와 같이 실행하면 음 재생.
Tempo = 120; % 템포 맞추기
%----------------------------
%----------------------------
beat1 = 60/Tempo*4;
beat2 = 60/Tempo*2;
beat4 = 60/Tempo; % 기본 4분음표
beat8 = 60/Tempo/2;
beat16 = 60/Tempo/4;
beat32 = 60/Tempo/8;
%----------------------------
%----------------------------
a(:,1) = beatSound('C',1,beat8); %1은 옥타브를 뜻함.
a(:,2) = beatSound('D',1,beat8);
a(:,3) = beatSound('E',1,beat8);
a(:,4) = beatSound('F',1,beat8);
a(:,5) = beatSound('G',1,beat8);
a(:,6) = beatSound('A',1,beat8);
a(:,7) = beatSound('B',1,beat8);
a(:,8) = beatSound('C',2,beat8);
%----------------------------
%%%%%%%%%%%%% 여기까지 %%%%%%%%%%%%%%%
return;

%%%%%%%%%%%% 여기서부터 %%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function [ madeSound ] = beatSound( tone, oct, beat )
%UNTITLED1 Summary of this function goes here
% Detailed explanation goes here

switch tone
case 'C'
freqSound = 264*2^((12*(oct-1)+0)/12);
case 'Db'
freqSound = 264*2^((12*(oct-1)+1)/12);
case 'D'
freqSound = 264*2^((12*(oct-1)+2)/12);
case 'Eb'
freqSound = 264*2^((12*(oct-1)+3)/12);
case 'E'
freqSound = 264*2^((12*(oct-1)+4)/12);
case 'F'
freqSound = 264*2^((12*(oct-1)+5)/12);
case 'Gb'
freqSound = 264*2^((12*(oct-1)+6)/12);
case 'G'
freqSound = 264*2^((12*(oct-1)+7)/12);
case 'Ab'
freqSound = 264*2^((12*(oct-1)+8)/12);
case 'A'
freqSound = 264*2^((12*(oct-1)+9)/12);
case 'Bb'
freqSound = 264*2^((12*(oct-1)+10)/12);
case 'B'
freqSound = 264*2^((12*(oct-1)+11)/12);
case 'R'
freqSound = 264*2^((12*(oct-1)-1)/12);
end

Fs = 44100; %Fs:샘플링주파수,
T = beat; % 지속시간. 몇분음표인지에 따라 다르다.
n = [0:(T*Fs)-1]';

%---------------------------- 음 생성
madeSound = sin(2*pi*freqSound*n/Fs);
%subplot(2,1,1), plot(madeSound);
%---------------------------- 자연스러운 음 감쇄적용
toneL = length(madeSound);
t = linspace(0,3,toneL)';
expFunc = exp(-t);
madeSound = madeSound.*expFunc;
%subplot(2,1,2), plot(madeSound);
%----------------------------
%----------------------------음 재생
wavplay(madeSound,Fs,'sync');
return;

2. 신호를 살짝 찍어보니 차츰차츰 신호의 세기를 줄이네요.
그 부분을 삭제하고 들어보니깐, 막귀인 저도 뚝뚝 끝기는 느김이 납니다.^^

grassman의 이미지

1. freqSound의 경우 입력된 음계를 확인 한 뒤에 그에 맞는 주파수를 계산하는 겁니다.

freqSound = 264*2^((12*(oct-1)+(도 = 0, 레b = 1, ...))/12);

264는 가온다의 주파수 값이고요. octave 값은 oct에 들어갑니다. octave가 올라가면 주파수는 정확히 2배 차이가 납니다. 따라서 그 사이의 음계는 전체 octave를 12로 나눈 것이고요. (이때 지수형태로 계산하지 않으면 소리가 이상해집니다. 2배씩 증가하기 때문에 지수 계산이 필요합니다. 264 * 2^(x) 형태가 되는 이유가 그겁니다.)

2. 자연스러운 음 감쇄 부분은 계산된 음의 amplitude 값을 지수적으로 감소시키는 부분입니다.
expFunc = exp(-t); 에서 계산된 값을 발생된 음에 곱해주면 (madeSound = madeSound.*expFunc;) amplitude가 지수적으로 감소하는 것을 확인할 수 있을 겁니다.

간만에 재미있는 코드였습니다. 감사합니다.

댓글 달기

Filtered HTML

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

BBCode

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param>
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

Textile

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • You can use Textile markup to format text.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Markdown

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • Quick Tips:
    • Two or more spaces at a line's end = Line break
    • Double returns = Paragraph
    • *Single asterisks* or _single underscores_ = Emphasis
    • **Double** or __double__ = Strong
    • This is [a link](http://the.link.example.com "The optional title text")
    For complete details on the Markdown syntax, see the Markdown documentation and Markdown Extra documentation for tables, footnotes, and more.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Plain text

  • HTML 태그를 사용할 수 없습니다.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 줄과 단락은 자동으로 분리됩니다.
댓글 첨부 파일
이 댓글에 이미지나 파일을 업로드 합니다.
파일 크기는 8 MB보다 작아야 합니다.
허용할 파일 형식: txt pdf doc xls gif jpg jpeg mp3 png rar zip.
CAPTCHA
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.