2015年12月26日 星期六

簡易 piano_GUI (使用GUIDE)







程式碼

function varargout = piano(varargin)
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, 'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @piano_OpeningFcn, 'gui_OutputFcn',  @piano_OutputFcn, ...
                   'gui_LayoutFcn',  [] , 'gui_Callback',   []);
if nargin && ischar(varargin{1})
    gui_State.gui_Callback = str2func(varargin{1});
end
if nargout
    [varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
    gui_mainfcn(gui_State, varargin{:});
end

function piano_OpeningFcn(hObject, ~, handles, varargin)
global ff Fs t m;
Fs = 11025; t = 0:1/Fs:(1-1/Fs); m = exp(-(0:Fs-1)/3/Fs);
pitch = 60:72;
ff = fix(440 * 2.^((pitch - 69)/12));
handles.output = hObject;
guidata(hObject, handles);                                      % Update handles structure

function varargout = piano_OutputFcn(~, ~, handles)
varargout{1} = handles.output;

function pushbuttonE_Callback(~, ~, ~)
global ff t Fs m;  y = sin(2 * pi * ff(5) * t).*m;  wavplay(y, Fs);

function pushbuttonC_Callback(~, ~, ~)
global ff t Fs m;  y = sin(2 * pi * ff(1) * t).*m;  wavplay(y, Fs);

function pushbuttonD_Callback(~, ~, ~)
global ff t Fs m;  y = sin(2 * pi * ff(3) * t).*m;  wavplay(y, Fs);

function pushbuttonF_Callback(~, ~, ~)
global ff t Fs m;  y = sin(2 * pi * ff(6) * t).*m;  wavplay(y, Fs);

function pushbuttonG_Callback(~, ~, ~)
global ff t Fs m;  y = sin(2 * pi * ff(8) * t).*m;  wavplay(y, Fs);

function pushbuttonA_Callback(~, ~, ~)
global ff t Fs m;  y = sin(2 * pi * ff(10) * t).*m;  wavplay(y, Fs);

function pushbuttonB_Callback(~, ~, ~)
global ff t Fs m;  y = sin(2 * pi * ff(12) * t).*m;  wavplay(y, Fs);

function pushbuttonC__Callback(~, ~, ~)
global ff t Fs m;  y = sin(2 * pi * ff(13) * t).*m;  wavplay(y, Fs);

function pushbutton1_Callback(~, ~, ~)
global ff t Fs m;  y = sin(2 * pi * ff(2) * t).*m;  wavplay(y, Fs);

function pushbutton2_Callback(~, ~, ~)
global ff t Fs m;  y = sin(2 * pi * ff(4) * t).*m;  wavplay(y, Fs);

function pushbutton3_Callback(~, ~, ~)
global ff t Fs m;  y = sin(2 * pi * ff(7) * t).*m;  wavplay(y, Fs);

function pushbutton4_Callback(~, ~, ~)
global ff t Fs m;  y = sin(2 * pi * ff(9) * t).*m;  wavplay(y, Fs);

function pushbutton5_Callback(~, ~, ~)
global ff t Fs m;  y = sin(2 * pi * ff(11) * t).*m;  wavplay(y, Fs);

function pushbutton6_Callback(~, ~, ~)

2015年12月22日 星期二

Cardioid Curve(心臟形曲線)



適當的加上axis off,可刪除座標軸。

再適當的加上
                 h1 = figure;
                set(h1, 'color','w');
可以將底色設成白色。


2015年12月16日 星期三

plottools命令調整繪圖視窗的輸入介面

在Command Window中鍵入plottools命令、或是在已開啟的繪圖視窗(Figure xx)的工具列中按下「Show Plot Tools and Dock Figure」快捷鍵


即可顯示繪圖視工具列和調整的輸入介面。



在「Figure Palette」之「New Subplots」中按下快捷鍵「2D Axes」或「3D Axes」即可產生2D或3D的繪圖區;若按下快捷鍵


則會產生磁磚狀的子圖樣式(create tiled subplots),選擇後繪圖視窗將呈現多張子圖的繪圖區。



選擇2×2後的結果,如下圖所示:



選取第一個Axes(no title),在屬性編輯器中將由Property Editor - Figure改為Property Editor – Axes,如下圖所示:


若再按下「Add  Data...」,將出現Add Data to Axes對話視窗,如下圖所示:


在X Data Source中輸入「[0:0.1:2*pi]」,在Y Data Source中輸入「cos([0:0.1:2*pi]).^2」,按下OK按鈕,即可繪製出函數曲線,此時在第一個Axes (no title)下方將出現藍色 ─ 及cos([0:0.1:2*pi]).^2 vs 0:0.1:2*pi字樣,如下圖所示。



此時若在Workspace視窗已有向量變數,將會在「Figure Palette」之「Variables」中顯示出已存在的變數,可將已存在的向量變數(或經MATLAB函數運算)當資料來源。旁邊的核取方塊

可以選擇該曲線是否可視。


點選第一個Axes (no title),可以看到Title(標題)-有時要適度放大繪圖視窗。


在Title裡鍵入「cos^{2}(t)」,結果如下圖所示。



在X Axis頁籤中的X Label輸入「\it{t}\rm(sec)」、X Limits由0 to 2*pi,結果如下圖所示。


在Y Axis頁籤中的Y Label輸入「Amplitude」、Y Limits由0 to 1,結果如下圖所示。


點選 ─ cos([0:0.1:2*pi]).^2 vs 0:0.1:2*pi,除了可以重新修改曲線的資料來源內容,也可修改Plot Type點選,如下圖所示。


並可依不同樣式調整繪製的參數。



若要顯示其它的参數,可按下「More Properties...」,


調整好圖形資料後,可在工具列中按下「Hide Plot Tools」快捷鍵


即可結束繪圖調整的輸入介面。


2015年7月11日 星期六

第六週課程----gradient()、quiver()、bar()、barh()、pie()、hist()、comet()、contour()、contourc()、contourf()、polar()、stairs()

11.梯度向量場的gradient()及quiver()


gradient()函數命令格式如下:
                                     [Ax,Ay,Az, ...] = gradient(A,hx,hy,hz, ...)
         其中 A 為 N-dimension 陣列,而hx、hy、hz, ...,分別為沿第一維度、第二維度、第三維度、...,方向變數的間格。


quiver()為繪製箭頭函數,其命令格式如下:
                                     quiver(X,Y)
         依據X、Y陣列中的X(i,j)、Y(i,j)數值,以類似複數 z = X(i,j)+jY(i,j)在座標(i,j)上繪製一具有方向與長度的箭頭。gradient()函數必須配合quiver()函數方可繪出梯度向量場圖形。
例如:

          >> [x, y] = meshgrid(-2:.2:2, -2:.2:2);                        %建立繪製x,y網格陣列
          >> A = x.^2.*y + x;
          >> [Ax, Ay] = gradient(A,.2,.2);
          >> contour(A), hold on, quiver(Ax,Ay), hold off      % contour為繪製等高線圖函數

12.長條圖bar()
bar()函數命令格式如下:
格式一:
                                     bar(Y,width)
         其中Y為一向量,其內容為每個長條圖形的高,而width為每個長條圖形的寬,預設值為0.8。width的數值建議小於、等於1,以避免條狀圖形互相重疊。
例如:
          >> y=[1 3 5 2];
          >> bar(y,1);      %長條圖形的寬為1
       


例如:
          >> y=[1 3 5 2];
          >> bar(y), colormap(hot)      %設定暖色系顏色


格式二: 
                                     bar(Y,width)
其中Y為一陣列,以Y的列為一群組,分別繪製以Y的行數的長條狀圖形。
例如:
          >> y=magic(4)
          y =
              16     2     3    13
               5    11    10     8
               9     7     6    12
               4    14    15     1
          >> bar(y)


格式三: 
                                     bar(X,Y,width)
其中X為一向量,Y可為向量或陣列,以X的內容指定水平軸的座標位置,若無X參數,則以1:size(Y,1)為水平軸的座標位置。
例如:
          >> y=magic(4)
          >> x = [2 6 8 11];
          >> bar(x,y)

格式四: 
                                     bar(X,Y,width,'type')
其中type可以為 'grouped' ,也可以是'stacked'。grouped可將Y的每列內容以同一群組長條狀圖形來繪製;stacked則可將同一群組長條狀圖形以堆疊方式繪製,內定值為grouped。
例如:
          >> y=magic(4)
          >> x = [2 6 8 11];
          >> bar(x,y,'stacked')
 

13.橫向長條圖barh()
barh()為bar()相似的函數,差別在於圖形以水平軸方式呈現。
          例如:
          >> y=magic(4)
          >> x = [2 6 8 11];
          >> barh(x,y,'stacked')

14.派形圖pie()
pie()函數命令格式如下:
                                     pie(X,explode,labels)
         其中X可為向量或陣列,而explode亦為向量或陣列,其維數或元素個數必須與X相同,當explode內容若不為0,則表示相對於X位置的圖示部分會與其他部分分開呈現。而labels是一個cell陣列,其大小須與X相同,且只能包含字串,主要是提供描述扇形的意義。若無labels則會顯示各扇形所佔面積的百分比。
例如:
          >> x = [20 60 10; 30 15 50; 10 35 30; 40 20 10];
          >> explode = [0 0 0 1 0 0 0 0 0 0 0 0];
          >> labels = {'Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec'};
          >> pie(x,explode,labels)



15.直方圖(histogram)hist()
hist()函數與bar()函數相似,但hist()函數可自行定義區間數。hist()函數命令格式如下:
                                     N = hist(X,n)
         其中X可為向量或陣列,而 n 為區間數,內定值為10。hist()函數會依X內容大小分成  n 個區間,若設有回傳參數 N,則 N 為 n 個區間內資料的個數;若無回傳參數,則會繪出各區間統計個數的直方圖。
例如:
          >> x = randn(100,1);
          >> N = hist(x,15)
          N =
            Columns 1 through 12
               2     1     4     5     6    11     6    16    20    14     8     1
            Columns 13 through 15
               4     0     2
          >> hist(x,15)

例如:
          >> x = randn(100,3);     %以行為群組
          >> hist(x,20)                  %回傳20*3的陣列, 並以深藍色表示每組第一筆統計值



16.極座標統計圖rose()
rose()與hist()函數相似,但rose()函數是以極座標方式呈現,但只針對向量內容做統計,也不能有輸出參數。rose()函數命令格式如下:
                                     rose(X,n)
         其中X為向量,而 n 為區間數,內定值為20。rose()函數也會依X內容大小分成  n 個區間,並繪出各區間統計個數的直方圖。
例如:
          >> x = randn(100,1);
          >> rose(x,30)
       


17.彗星圖comet()---俱動態畫的繪製圖形
comet()函數命令格式如下:
                                     comet(x,y,p)
         其中x、y皆為向量,而 p為彗星尾參數,內定值為0.1。彗星尾長定義為 p*length(y)。
例如:
          >> t = linspace(0,4*pi,10000); comet(t, sin(4*t)+cos(log(t/100)))     %綠色為彗星尾



18.等高線圖contour()、contourc()、contourf()
contour()函數命令格式如下:
         格式一:
                                     contour(z,n)
         其中 z 為(m*n)陣列,而 n 為等高線個數,若無 n 參數,Matlab會自動調整等高線數。等高線圖之水平軸座標為1~size(z,2),即1~n;垂直軸座標為1~size(z,1),即1~m。
例如:
          >> z=[70 2 12 52 38 48; 8 28 33 17 10 15; 65 6 40 38 33 40; 4 36 29 13 18 11]
          z =
              70     2    12    52    38    48
               8    28    33    17    10    15
              65     6    40    38    33    40
               4    36    29    13    18    11
          >> contour(z,5)

格式二:
                                     contour(x,y,z,n)
         其中 x 與 y 分別為水平軸座標、垂直軸座標陣列。
例如:
         >> [x,y,z] = peaks(50);
         >> contour(x,y,z,15)

格式三:
                                     C=contour(x,y,z,[z1,z2,...,zn])
         其中 z1,z2,...,zn為指定的等高線值,而C為等高線回傳陣列。若只要繪出單一數值的等高線,必須指定兩個相同的等高線值,e.g.  contour(x,y,z,[2, 2])就只繪出數值2的等高線。
例如:
         >> [x,y,z] = peaks(50);
         >> C=contour(x,y,z,[-2, 0, 2, 10]);
         >> clabel(C)                           % clabel()函數可標示出等高線數值
contourf()函數與contourc()函數的用法與contour()函數相似,主要差異是contourf()函數會在等高圍線中填滿顏色;而contourc()函數只回傳計算等高線的陣列不會繪圖,並且x與y皆要為向量,不可為陣列。
例如:
         >> [x,y,z] = peaks(50);
         >> C=contourf(x,y,z,[-2, 0, 2, 10]);
         >> clabel(C)

註:
clabel()函數格式如下:
格式一:
                                     clabel(C,[z1,z2,...,zn])
         其中 C 為等高線陣列,可在等高線圖上標記高度數值。其中 z1,z2,...,zn為指定的等高線值,此參數必須配合等高線計算函數contour()、contourf()或contourc,此參數可以省略。

          格式二:
                                     clabel(C,'manual')
其中 'manual' 是指可配合滑鼠控制,在欲標示的等高線上標註數值。回返可按下 "Enter" 鍵。

19.極座標曲線圖polar()
polar()函數可加入線條規範語法(Line specification syntax)來指定繪圖曲線的樣式。polar()函數命令格式如下:
                                              polar(theta,r)
         其中 theta 為角度向量,而 r 為半徑。
例如:
         >> t=0:0.01:2*pi;
         >> polar(t,3*sin(5*t)+1, '--r');