Mais conteúdo relacionado Semelhante a Matlab演習 (20) Matlab演習 6. 行列計算
入力行列の定義
直接入力によるデータ定義
>>a=[1 ,2 ,3 ;4 ,5 ,6 ]
a=
123
456
1. 要素全体を[ ]で囲む
2. 各列をスペースまたはカ
ンマで区切る
3. 各行をセミコロンで区切
る
>>b=[1:10]
1 2 3 4 5 6 7 8 9 10
>>c=[0:2:10]
0 2 4 6 8 10
コロンを用いると等間隔ベ
クトルを定義できる
16. グラフの描画
例:課題18
• %_/_/_/_/_/_/_/
• %Sa(t)の描画
• %_/_/_/_/_/_/_/
• %tの値についてifで場合分け
• for ii=1:length(t)
•
•
•
•
•
•
•
•
•
•
•
•
•
% 0 ≤ |𝑡| ≤ 1の時
if 0<=abs(t(ii))&&abs(t(ii))<1
y2(ii)=1-2*abs(t(ii)).^2+abs(t(ii)).^3;
%1 ≤ |𝑡| ≤ 2 の時
elseif 1<=abs(t(ii))&&abs(t(ii))<2
y2(ii)=4-8*abs(t(ii))+5*abs(t(ii)).^2-abs(t(ii)).^3;
%その他
else
y2(ii)=0;
end
end
subplot(3,1,2);
plot(t,y2);
tが1行10列の行列なら
length(t)=10である
abs(t)でtの絶対値をとる
17. グラフの描画
例:課題18
t= -10:0.1:10;
y1=sinc(t);
subplot(3,1,1);
plot(t,y1);
I = find((0<=abs(t)&abs(t)<1));
J = find(1<=abs(t)&abs(t)<2);
y2=zeros(size(t));
y2(I)=1-2*abs(t(I)).^2+abs(t(I)).^3;
y2(J)=4-8*abs(t(J))+5*abs(t(J)).^2-abs(t(J)).^3;
subplot(3,1,2);
plot(t,y2);
y3=abs(y1-y2);
subplot(3,1,3);
plot(t,y3
このようなスクリプトを
for文なしで書けることも
MATLABの魅力
→計算速度を上げるため
には必須
25. 画像処理
200 180 165 150
235 195 155
70
220 209
98
50
202
85
69
47
131
35
38
40
113
47
50
17
115 110
9
15
120
14
15
14
113
23
30
25
121
34
30
8
150 110
11
12
126
10
17
R
カラー画像なら値は各画素に
R,G,Bの3つの成分を持つ
→3次元配列
G
B
15
28. 標本化
∞
𝑥 𝑠 𝑡 = 𝑥(𝑡)
𝛿(𝑡 − 𝑛𝑇)
𝑛=−∞
標本化
1/サンプリング周波数
とびとびのデルタ関数で表す
→
間のデータはどうするのか?
31. 講義の課題19
clear all
close all
[in,Fs] = wavread(‘sample.wav’)
l_in = length(in);
l_out1 = floor(l_in/2);
out1 = zeros(1,l_out1);
for i= 1:l_out1
out1(i) = in(2*i);
end
wavwrite(out1,Fs/2,'down.wav');
l_out2 = l_in*2;
out2 = zeros(1,l_out2);
for i= 1:l_in
out2(i*2) = in(i);
end
h1=fir1(40,(Fs/2)/Fs,'low');
out2=conv(out2,h1);
out2=out2(1:l_out2);
wavwrite(out2,Fs*2,'up.wav');
32. 講義の課題19
clear all
close all
[in,Fs] = wavread(‘sample.wav’)
l_in = length(in);
%_/_/_/_/_/_/_/_/_/_/
%ダウンサンプリング
%_/_/_/_/_/_/_/_/_/_/
%あらかじめ入力の半分の長さを持った行列を作成する(値は全て0)
l_out1 = floor(l_in/2);
out1 = zeros(1,l_out1);
%入力のサンプルを一個おきに出力に代入する
for i= 1:l_out1
out1(i) = in(2*i);
end
%音声の書き出し サンプリング周波数は2分の1
wavwrite(out1,Fs/2,'down.wav');
38. 画像の補間
%画像の読み込み
img=imread('en/lenasmall.jpg');
%グレースケール化
img=rgb2gray(img);
%最近傍補間
imgA=imresize(img,3,'nearest');
%線形補間
imgB=imresize(img,3,'bilinear');
%双3次補間
imgC=imresize(img,3,'bicubic');
%画像の表示
subplot(1,3,1);
imshow(imgA);
subplot(1,3,2);
imshow(imgB);
subplot(1,3,3);
imshow(imgC);
imresize(img,3,'nearest');
画像の行列,拡大率,補間方法
を引数に取る
subplot(1,3,1);ならば
1行3列に図を並べる
1
2
3
3つ目の引数が1ならば
上図の1の部分に図が入
る
41. 離散フーリエ変換
時間領域の離散信号を周波数領域の離散スペクトルへ変換す
るもの
clear all
close all
[y1,fs]=wavread('en/aiueo.wav');
x1=1:length(y1);
x1=x1/fs;
subplot(2,1,1);
plot(x1,y1);
時間波形
fftsize=2^ceil(log2(length(y1)));
y2=20*log10(abs(fft(y1,fftsize)));
x2=linspace(0,fs,fftsize);
subplot(2,1,2)
plot(x2(1:fftsize/2),y2(1:fftsize/2));
パワースペクトル
45. スペクトログラム
clear all
close all
[x,fs]=wavread('aiueo.wav');
frameMs=30;
periodMs=10;
frame=round(fs*frameMs/1000);
period=round(fs*periodMs/1000);
fftlength=2^ceil(log2(frame));
range=1:period:(length(x)-frame);
spec=zeros(fftlength/2,length(range));
for ii=1:length(range)
temp=20*log10(abs(fft(x([1:frame]+(ii-1)*period).*blackman(frame),fftlength))+eps);
spec(:,ii)=temp(1:fftlength/2+1);
end
xx=linspace(0,length(x)/fs,size(spec,2));
yy=linspace(0,fs/2,size(spec,1));
surface(xx,yy,spec);shading flat;
46. スペクトログラム
• frame…切り出すフレームの大きさ.
ここでは30ms秒分のフレームを切り
出している
clear all
close all
[x,fs]=wavread('aiueo.wav');
frameMs=30;
periodMs=10;
frame=round(fs*frameMs/1000);
period=round(fs*periodMs/1000);
fftlength=2^ceil(log2(frame));
range=1:period:(length(x)-frame);
spec=zeros(fftlength/2,length(range));
• period…for文一回あたりに動かすフ
レーム.ここでは10ms分のフレーム
を動かす
• fftlength…変換後のサンプル数.こ
の時サンプル数は2の累乗が最も高速
である
• range…切り出しの開始フレームのベ
クトル
• spec…出力されたスペクトログラム
を格納するための変数
47. スペクトログラム
• 以上のことを図にすると…
for ii=1:length(range)
temp=20*log10(abs(fft(x([1:frame]+…
(ii-1)*period).*blackman(frame),fftlength))+eps);
spec(:,ii)=temp(1:fftlength/2+1);
end
xx=linspace(0,length(x)/fs,size(spec,2));
yy=linspace(0,fs/2,size(spec,1));
surface(xx,yy,spec);shading flat;
…
30ms
10ms
0 10 20…
range
• 窓関数は今後の講義で(おそらく)出てくる
(ここではblackman windowが使われている)
50. 画像のパワースペクトル
clear all
close all
img = imread('en/renga.bmp');
img=rgb2gray(img);
subplot(2,2,1);
imshow(img);
%2次元フーリエ変換
im=fft2(img);
%パワースペクトルの表示
[h, w]=size(im);
nw=floor(w/2);
nh=floor(h/2);
imout=[im(nh+1:h,nw+1:w),im(nh+1:h,1:nw);…
[im(1:nh,nw+1:w),im(1:nh,1:nw)]];
subplot(2,2,2);
imshow(20*log10(abs(imout)),[0 255]);
%縦方向の周波数をゼロに置き換える
im_arranged=im;
im_arranged(:,w-5:w)=0;
im_arranged(:,1:5)=0;
im_arranged(1,1)=im(1,1);
%置き換えたものを表示
imout2=[im_arranged(nh+1:h,nw+1:w),…
im_arranged(nh+1:h,1:nw);…
[im_arranged(1:nh,nw+1:w),…
im_arranged(1:nh,1:nw)]];
subplot(2,2,4);
imshow(20*log10(abs(imout2)),[0 255]);
%逆2次元フーリエ変換
img2 = abs(ifft2(im_arranged));
%逆フーリエ変換したものを表示
subplot(2,2,3)
imshow(uint8(img2));
51. 画像のパワースペクトル
clear all
close all
img = imread('en/renga.bmp');
img=rgb2gray(img);
subplot(2,2,1);
imshow(img);
%2次元フーリエ変換
im=fft2(img);
%パワースペクトルの表示
[h, w]=size(im);
nw=floor(w/2);
nh=floor(h/2);
imout=[im(nh+1:h,nw+1:w),im(nh+1:h,1:nw);…
[im(1:nh,nw+1:w),im(1:nh,1:nw)]];
subplot(2,2,2);
imshow(20*log10(abs(imout)),[0 255]);
• fft2で二次元高速フーリエ変換
• fftsizeを特に指定しなければ入力
と出力のサンプル数は同じになる
• パワースペクトルを見やすいものに
するために直流成分を画像中央に
持っていく この時対称性より行列
を図のように入れ替えればよい
• つまりできた画像をA,B,C,Dに分割
し[D,C;B,A]のようにする
元画像
入れ替えた画像
A
B
D
C
C
D
B
A
52. 画像のパワースペクトル
%縦方向の周波数をゼロに置き換える
im_arranged=im;
im_arranged(:,w-5:w)=0;
im_arranged(:,1:5)=0;
im_arranged(1,1)=im(1,1);
%置き換えたものを表示
imout2=[im_arranged(nh+1:h,nw+1:w),…
im_arranged(nh+1:h,1:nw);…
[im_arranged(1:nh,nw+1:w),…
im_arranged(1:nh,1:nw)]];
subplot(2,2,4);
imshow(20*log10(abs(imout2)),[0 255]);
%逆2次元フーリエ変換
img2 = abs(ifft2(im_arranged));
%逆フーリエ変換したものを表示
subplot(2,2,3)
imshow(uint8(img2));
• 再び並べ替え前の画像を用いる
• ここでは横方向の波を消すためにy軸
に近い点を0とする
• ただし直流成分も0にすると画像が全
体的に暗くなるのでその部分は残す
• 逆二次元高速フーリエ変換はifft2
60. ローパスフィルタ
𝑧 𝑛 = 𝑥 𝑛 ∗ 𝑦 𝑛 ⇔ 𝑍 𝑘 = 𝑋 𝑘 𝑌[𝑘]
×
=
周波数領域では畳み込みは1対1で掛け合わす
61. ローパスフィルタ
• clear all
• xx=linspace(0,fs/1000,length(x));
• close all
•
•
• plot(xx(1:fftlength),in(1:fftlength),'b','linewidth',2);
• [x,fs] = audioread('akstn.wav');
• hold on;
• fftlength=length(x)/2;
• plot(xx(1:fftlength),out1(1:fftlength),'g','linewidth',2);
• filterSize=40;
• plot(xx(1:fftlength),out2(1:fftlength),'r','linewidth',2);
• in=20*log10(abs(fft(x)));
•
• h1=fir1(filterSize,500/(fs/2),'low');
• y1=conv(x,h1);
• y1=y1(1:length(x));
• out1=20*log10(abs(fft(y1)));
•
62. ローパスフィルタ
• clear all
• close all
•
• [x,fs] wavread('aiueo.wav');
• fftlength=length(x)/2;
• filterSize=40;
• in=20*log10(abs(fft(x)));
•
• h1=fir1(filterSize,500/(fs/2),'low');
• y1=conv(x,h1);
• y1=y1(1:length(x));
• out1=20*log10(abs(fft(y1)));
•
• fir1でフィルタ作成
一つ目の引数にフィルタサイズ(40く
らい)
二つ目の引数にカットオフ周波数
ここで設定する周波数は(カットオフ周
波数)/(ナイキスト周波数)であるこ
とに注意(ナイキスト周波数はサンプリ
ング周波数の2分の1)
三つ目の引数にフィルター タイプ
ここでハイパスやローパスを決める
• convで畳み込み
63. ローパスフィルタ
• xx=linspace(0,fs/1000,length(x));
•
• plot(xx(1:fftlength),in(1:fftlength),'b','linewidth',2);
• hold on;
• plot(xx(1:fftlength),out1(1:fftlength),'g','linewidth',2);
• plot(xx(1:fftlength),out2(1:fftlength),'r','linewidth',2);
• plotの後に設定を追加する
ことで色を付けたり実線を
点線にしたりすることがで
きる 詳しくはhelpコマ
ンドなどで
• 一つ目のplotの後にhold
on と打つことによりもと
もと書いてあったグラフに
上書きできる
66. 平滑化フィルタ
clear all
close all
im=imread('lena.jpg');
im= rgb2gray(im);
subplot(1,2,1);
imshow(im);
%前処理
%画像の読み込み
%グレ-スケール化
%元画像の表示
im=double(im);
%uint8→double型へ
f=[1,1,1;1,1,1;1,1,1]/9; %フィルタの作成
imout=filter2(f,im,'same');%元画像とフィルタの畳み込み
imout=uint8(imout);
%double→uint型へ
subplot(1,2,2);
imshow(imout);
%平滑化した画像の表示