MATLAB 中的视听处理与图形用户界面开发
1. 声音文件处理
声音信号是连续信号,经过采样后变成离散信号。采样频率指单位时间(如每秒)内的采样次数,通常用赫兹(Hz)衡量。在 MATLAB 里,离散声音信号由向量表示,频率以赫兹为单位。MATLAB 有多个 MAT 文件存储不同声音的信号向量(变量
y
)和频率(变量
Fs
),像
chirp
、
gong
、
laughter
等。
1.1 播放声音
MATLAB 有内置函数
sound
可将声音信号发送到输出设备(如扬声器)。函数调用格式为:
>> sound(y,Fs)
例如,要播放锣声,可按以下步骤操作:
>> load gong
>> sound(y,Fs)
1.2 绘制声音信号
声音信号的振幅存储在变量
y
中,范围应在 -1 到 1 之间。可使用
plot
函数显示数据,以下脚本创建子图显示
chirp
和
train
的信号:
% Display the sound signals from chirp and train
load chirp
subplot(2,1,1)
plot(y)
title('Chirp')
load train
subplot(2,1,2)
plot(y)
title('Train')
1.3 不同音频文件格式处理
sound
函数的第一个参数可以是
n×2
矩阵以实现立体声。调用
sound
函数时可省略第二个参数,此时默认采样频率为 8192 Hz。MATLAB 还有其他处理声音或音频文件的函数,常见音频文件格式及对应处理函数如下:
| 文件格式 | 开发公司 | 对应处理函数 | 默认频率 |
| ---- | ---- | ---- | ---- |
| .au | Sun Microsystems |
auread
(读取)、
auwrite
(写入) | - |
| .wav | Microsoft |
wavread
(读取)、
wavrecord
(录制)、
wavwrite
(写入)、
wavplay
(播放) | 11025 Hz |
2. 句柄图形介绍
在句柄图形中,所有图形由不同对象组成,每个对象都有唯一的句柄,用于引用该对象。
2.1 图形对象及其属性
图形对象包括图形基元(如线条和文本)以及用于定位对象的坐标轴。对象按层次结构组织,每个对象都有相关属性,这是面向对象编程的基础,子对象通常继承父对象的属性。其层次结构如下:
graph LR
A[Figure] --> B[Axes]
B --> C[Core Objects]
B --> D[Plot Objects]
2.2 核心对象
MATLAB 中的核心对象是基本的图形基元,包括
line
、
text
、
rectangle
、
patch
和
image
。以下是部分核心对象的示例:
2.2.1 线条对象
>> x = -2*pi: 1/5 : 2*pi;
>> y = sin(x);
>> hl = line(x,y,'LineWidth', 3, 'Color', [0.5 0.5 0.5])
可使用
get
函数查看对象属性:
>> get(hl)
2.2.2 文本对象
>> x = –4:0.2:4;
>> y = sin(x);
>> hp = line(x,y,'LineWidth',3);
>> thand = text(2,0,'Sin(\pi)\rightarrow')
使用
set
函数修改文本框属性:
>> set(thand,'BackgroundColor',[0.8 0.8 0.8],...
'EdgeColor',[1 0 0])
2.2.3 矩形对象
>> recthand = rectangle;
>> get(recthand)
可设置矩形对象的属性:
>> rh = rectangle('Position', [0.2, 0.2, 0.5, 0.8],...
'Curvature',[0.5, 0.5]);
>> axis([0 1.2 0 1.2])
>> set(rh,'Linewidth',3,'LineStyle',':')
2.2.4 面片对象
polyhedron.vertices = [
0 0 0
1 0 0
0 1 0
0.5 0.5 1];
polyhedron.faces = [
1 2 3
1 2 4
1 3 4
2 3 4];
pobj = patch(polyhedron, ...
'FaceColor',[0.8, 0.8, 0.8],...
'EdgeColor','black');
3. 图像处理
图像在 MATLAB 中通常用矩阵表示,矩阵元素对应图像的像素,像素颜色有两种基本表示方法:
3.1 真彩色(RGB)
真彩色矩阵是三维矩阵,前两个维度表示像素坐标,第三个索引表示颜色分量(
(:,:,1)
为红色,
(:,:,2)
为绿色,
(:,:,3)
为蓝色)。例如,创建一个 2×2 的真彩色图像:
>> mat(1,1,1) = 255;
>> mat(1,1,2) = 0;
>> mat(1,1,3) = 0;
>> mat(1,2,1) = 0;
>> mat(1,2,2) = 0;
>> mat(1,2,3) = 255;
>> mat(2,1,1) = 0;
>> mat(2,1,2) = 255;
>> mat(2,1,3) = 0;
>> mat(2,2,1) = 0;
>> mat(2,2,2) = 0;
>> mat(2,2,3) = 0;
>> mat = uint8(mat);
>> image(mat)
3.2 颜色映射索引
索引表示法使用一个
m×n
的整数矩阵,每个整数是颜色映射矩阵的行索引。颜色映射矩阵是
p×3
的矩阵,存储红、绿、蓝分量。例如,显示
jet
颜色映射的 64 种颜色:
>> cmap = reshape(1:64, 8,8)
>> image(cmap)
3.3 图像文件处理
使用
imread
函数读取图像文件,如 JPEG 文件:
>> myimage1 = imread('Fishing_1.JPG');
>> size(myimage1)
>> image(myimage1)
可通过操作矩阵中的数值改变图像,如将图像变暗:
>> dimmer = 0.75*myimage1;
>> image(dimmer)
4. 图形用户界面介绍
图形用户界面(GUIs)允许用户通过图形界面(如按钮、滑块、单选按钮等)进行输入,是面向对象编程的一个例子,存在层次结构。
4.1 创建 GUI 的基本方法
在 MATLAB 中有两种创建 GUI 的基本方法:
1. 从头编写 GUI 程序。
2. 使用内置的图形用户界面开发环境(GUIDE)。GUIDE 可让用户图形化布局 GUI,MATLAB 自动生成代码,但为了理解和修改代码,需掌握底层编程概念,下面重点介绍编程方法。
4.2 图形窗口与属性设置
图形窗口是任何 GUI 的父对象,调用
figure
函数会弹出一个空白图形窗口。例如:
>> f = figure;
使用
get
函数可查看默认属性,使用
set
函数可更改属性,如:
>> get(f)
>> set(f, 'Color', 'white', 'Position', [300, 400, 450, 250])
创建 GUI 时,通常先创建父图形窗口并使其不可见,添加所有用户界面对象并设置属性后,再使其可见。
4.3 简单 GUI 示例
以下是一个仅包含静态文本框的简单 GUI 示例:
function simple_gui
% This is a simple GUI that just has a static text box
% Create the GUI but make it invisible for now while
% it is being initialized
f = figure('Visible', 'off', 'color', 'white', 'Position',...
[300, 400, 450, 250]);
htext = uicontrol('Style', 'text', 'Position',...
[200, 50, 100, 25], 'String', 'My First GUI string');
% Put a name on it and move to the center of the screen
set(f, 'Name', 'Simple GUI')
movegui(f, 'center')
% Now the GUI is made visible
set(f, 'Visible', 'on');
end
4.4 带可编辑文本框的 GUI 示例
此示例允许用户在可编辑文本框中输入字符串,按下回车键后,字符串将以红色大字体显示:
function gui_with_editbox
% This is a GUI that has an editable text box
% and a callback function that prints the user’s
% string in red
% Create the GUI but make it invisible for now
f = figure('Visible', 'off', 'color', 'white', 'Position',...
[360, 500, 800, 600]);
% Put a name on it and move it to the center of the screen
set(f, 'Name', 'GUI with editable text')
movegui(f, 'center')
% Create two objects: a box where the user can type and
% edit a string and also a text title for the edit box
hsttext = uicontrol('Style', 'text',...
'BackgroundColor', 'white',...
'Position', [100, 425, 400, 55],...
'String', 'Enter your string here');
huitext = uicontrol('Style', 'edit',...
'Position', [100, 400, 400, 40],...
'Callback', @callbackfn);
% Now the GUI is made visible
set(f, 'Visible', 'on');
% Call back function
function callbackfn(source, eventdata)
set([hsttext huitext], 'Visible', 'off');
% Get the string that the user entered and print
% it in big red letters
printstr = get(huitext, 'String');
hstr = uicontrol('Style', 'text',...
'BackgroundColor', 'white',...
'Position', [100, 400, 400, 55],...
'String', printstr,...
'ForegroundColor', 'Red', 'FontSize', 30);
set(hstr, 'Visible', 'on')
end
end
流程如下:
graph TD
A[创建不可见图形窗口] --> B[创建静态文本框和可编辑文本框]
B --> C[使图形窗口可见]
C --> D[用户输入字符串并回车]
D --> E[调用回调函数]
E --> F[隐藏原文本框,显示红色大字体字符串]
4.5 带按钮的 GUI 示例
此示例在 GUI 中添加了一个按钮,用户输入字符串后,点击按钮才会触发回调函数:
function gui_with_pushbutton
% This is a GUI with an editable text box and a pushbutton
% Create the GUI but make it invisible for now while
% it is being initialized
f = figure('Visible', 'off', 'color', 'white', 'Position',...
[360, 500, 800, 600]);
hsttext = uicontrol('Style', 'text', 'BackgroundColor', 'white',...
'Position', [100, 425, 400, 55],...
'String', 'Enter your string here');
huitext = uicontrol('Style', 'edit', 'Position', [100, 400, 400, 40]);
set(f, 'Name', 'GUI with pushbutton')
movegui(f, 'center')
% Create a pushbutton that says Push me!!
hbutton = uicontrol('Style', 'pushbutton', 'String',...
'Push me!!', 'Position', [600, 50, 150, 50],...
'Callback', @callbackfn);
% Now the GUI is made visible
set(f, 'Visible', 'on');
% Call back function
function callbackfn(source, eventdata)
set([hsttext huitext hbutton], 'Visible', 'off');
printstr = get(huitext, 'String');
hstr = uicontrol('Style', 'text', 'BackgroundColor',...
'white', 'Position', [100, 400, 400, 55],...
'String', printstr,...
'ForegroundColor', 'Red', 'FontSize', 30);
set(hstr, 'Visible', 'on')
end
end
4.6 带滑块的 GUI 示例
以下示例创建了一个滑块,可显示滑块的最小值、最大值和当前值:
function gui_slider
% This is a GUI with a slider
f = figure('Visible', 'off', 'color', 'white', 'Position',...
[360, 500, 300, 300]);
% Minimum and maximum values for slider
minval = 0;
maxval = 5;
% Create the slider object
slhan = uicontrol('Style', 'slider', 'Position',...
[80, 170, 100, 50],...
'Min', minval, 'Max', maxval, 'Callback', @callbackfn);
% Text boxes to show the minimum and maximum values
hmintext = uicontrol('Style', 'text', 'BackgroundColor', 'white',...
'Position', [40, 175, 30, 30], 'String', num2str(minval));
hmaxtext = uicontrol('Style', 'text', 'BackgroundColor', 'white',...
'Position', [190, 175, 30, 30], 'String', num2str(maxval));
% Text box to show the current value (off for now)
hsttext = uicontrol('Style', 'text', 'BackgroundColor', 'white',...
'Position', [120, 100, 40, 40], 'Visible', 'off');
set(f, 'Name', 'Slider Example')
movegui(f, 'center')
set(f, 'Visible', 'on');
% Call back function displays the current slider value
function callbackfn(source, eventdata)
num = get(slhan, 'Value');
set(hsttext, 'Visible', 'on', 'String', num2str(num))
end
end
流程如下:
graph TD
A[创建不可见图形窗口] --> B[设置滑块最小、最大值]
B --> C[创建滑块和文本框]
C --> D[使图形窗口可见]
D --> E[用户操作滑块]
E --> F[调用回调函数,显示当前值]
通过以上内容,我们了解了 MATLAB 中声音文件处理、句柄图形、图像处理和图形用户界面开发的相关知识和操作方法。在实际应用中,可根据需求灵活运用这些功能,开发出满足各种需求的程序。
超级会员免费看
652

被折叠的 条评论
为什么被折叠?



