基于Matlab音乐流派识别检测处理系统GUI这个系统是一个用Matlab的音乐流派识别系统系统背后用的是Kaggle的GTZAN音乐数据集里面包含了10种常见音乐类型迪斯科、古典、金属、爵士、蓝调、雷鬼、流行、嘻哈、乡村、摇滚。每种类型有300首歌作为训练集一共3000多个音频训练、测试集每个文件夹有15个音乐一共150个测试集。系统的核心识别逻辑是先对音频进行MFCC特征提取简单来说就是把音乐转换成一个能看出风格特征的频谱图然后再用CNN也就是卷积神经网络来进行训练和识别。这种方法在声音识别领域很常见效果也比较稳能有效地从音乐中分辨出风格差异。使用系统时可以直接导入一首音乐文件然后播放它。系统支持播放、暂停、停止等常规操作还可以自动生成音乐的时域图也就是声音波形图和频域图显示不同频率强度的图方便你观察音乐的特性。点击“开始识别”按钮后系统会立即分析当前音乐的音频特征并在界面中显示识别结果包括音乐流派类型和音频的路径等信息整个过程直观。这是一个基于 MATLAB 的音乐流派识别检测处理系统 GUI 的完整代码实现。系统特点核心算法MFCC (梅尔频率倒谱系数) 特征提取 CNN (卷积神经网络) 分类。数据集支持架构设计完全兼容 GTZAN 数据集 (10种流派Blues, Classical, Country, Disco, Hip-hop, Jazz, Metal, Pop, Reggae, Rock)。功能完备音频导入、播放、暂停、停止。实时绘制 时域波形图 和 频域频谱图。一键识别显示流派结果及置信度。无需工具箱依赖为了确保代码在任何安装了基础 MATLAB 的电脑上都能运行避免用户没有 Deep Learning Toolbox 或 Audio Toolbox本代码使用了纯 MATLAB 原生函数实现 MFCC 计算和简易 CNN 推理逻辑模拟训练好的模型权重加载过程。使用方法将以下代码保存为 MusicGenreGUI.m。在 MATLAB 命令行输入 MusicGenreGUI 运行。关于模型训练由于无法在此处提供几百兆的 .mat 模型文件代码中包含了一个 train_model_demo.m 的逻辑片段在代码注释中说明或者系统会在首次运行时生成一个随机初始化的演示模型识别结果为随机演示若要真实识别需替换为真实训练好的 net 和 weights。注为了让你直接看到效果我在代码中内置了一个“模拟识别”模式如果你没有训练好的模型它会模拟识别过程并给出一个示例结果同时展示完整的波形和频谱分析功能。MATLAB 代码 (MusicGenreGUI.m)function MusicGenreGUI()% 基于 MATLAB 的音乐流派识别检测处理系统 GUI% 核心MFCC CNN (模拟/预留接口)% 数据集GTZAN (10类)%% 1. 初始化全局变量 persistent audioData fs model netConfig; if isempty(audioData), audioData []; end if isempty(fs), fs []; end if isempty(model), % 初始化模拟模型结构 (实际使用时应 load(trained_cnn_model.mat)) model.isTrained false; model.classes {Blues, Classical, Country, Disco, Hip-hop, ... Jazz, Metal, Pop, Reggae, Rock}; % 模拟权重占位 model.weights rand(10, 1); end %% 2. 创建 GUI 界面 hFig figure(Name, 音乐流派识别系统 (GTZAN-CNN), ... NumberTitle, off, ... Position, [100, 100, 900, 700], ... MenuBar, none, ... ToolBar, none, ... Resize, off, ... Color, [0.94, 0.94, 0.94]); % 标题 uicontrol(Style, text, String, 音乐流派智能识别系统, ... FontSize, 20, FontWeight, bold, ... Position, [300, 640, 300, 40], ... BackgroundColor, [0.94, 0.94, 0.94], ... HorizontalAlignment, center); uicontrol(Style, text, String, 基于 MFCC 特征与 CNN 深度学习 (GTZAN 数据集), ... FontSize, 10, FontAngle, italic, ... Position, [280, 615, 340, 20], ... BackgroundColor, [0.94, 0.94, 0.94], ... HorizontalAlignment, center); % --- 左侧控制面板 --- panelCtrl uipanel(Parent, hFig, Title, 控制与识别, ... Position, [20, 20, 260, 580], ... BackgroundColor, white); % 按钮组 btnWidth 220; btnHeight 35; gap 50; startY 520; uicontrol(Parent, panelCtrl, Style, pushbutton, String, 导入音乐文件, ... Position, [20, startY, btnWidth, btnHeight], ... FontSize, 12, Callback, importCallback); uicontrol(Parent, panelCtrl, Style, pushbutton, String, 播放, ... Position, [20, startY-gap, btnWidth, btnHeight], ... FontSize, 12, Callback, playCallback); uicontrol(Parent, panelCtrl, Style, pushbutton, String, 暂停, ... Position, [20, startY-2*gap, btnWidth, btnHeight], ... FontSize, 12, Callback, pauseCallback); uicontrol(Parent, panelCtrl, Style, pushbutton, String, 停止, ... Position, [20, startY-3*gap, btnWidth, btnHeight], ... FontSize, 12, Callback, stopCallback); uicontrol(Parent, panelCtrl, Style, pushbutton, String, 开始识别, ... Position, [20, startY-4.5*gap, btnWidth, btnHeight10], ... FontSize, 14, FontWeight, bold, BackgroundColor, [0.2, 0.6, 0.8], ... ForegroundColor, white, Callback, recognizeCallback); % 结果显示区域 uicontrol(Parent, panelCtrl, Style, text, String, 识别结果:, ... Position, [20, 180, 100, 25], FontSize, 12, FontWeight, bold); hResultGenre uicontrol(Parent, panelCtrl, Style, text, String, --, ... Position, [20, 150, 220, 25], FontSize, 14, FontWeight, bold, ... ForegroundColor, [0.8, 0.2, 0.2], HorizontalAlignment, center); hResultConf uicontrol(Parent, panelCtrl, Style, text, String, 置信度: --%, ... Position, [20, 120, 220, 20], FontSize, 11, ... HorizontalAlignment, center); hFilePath uicontrol(Parent, panelCtrl, Style, text, String, 路径: 未选择, ... Position, [20, 60, 220, 40], FontSize, 9, ... Max, 2, HorizontalAlignment, left); % --- 右侧绘图面板 --- panelPlot uipanel(Parent, hFig, Title, 信号分析 (时域 频域), ... Position, [300, 20, 580, 580], ... BackgroundColor, white); % 时域图 axTime axes(Parent, panelPlot, Position, [0.1, 0.55, 0.8, 0.35]); title(axTime, 时域波形 (Time Domain)); xlabel(时间 (s)); ylabel(振幅); grid on; box on; % 频域图 (频谱/MFCC 可视化) axFreq axes(Parent, panelPlot, Position, [0.1, 0.1, 0.8, 0.35]); title(axFreq, 频域频谱 / MFCC 特征 (Frequency Domain)); xlabel(频率条带/帧); ylabel(强度/系数); grid on; box on; % 状态栏 hStatus uicontrol(Parent, hFig, Style, text, String, 就绪 | 请导入音乐文件, ... Position, [20, 5, 860, 20], HorizontalAlignment, center); %% 3. 回调函数定义 function importCallback(~, ~) [file, path] uigetfile({.wav;.mp3.flac;.aiff, Audio Files; ., All Files}, 选择音乐文件); if isequal(file, 0) return; end fullPath fullfile(path, file); set(hFilePath, String, [路径: , fullPath]); set(hStatus, String, 正在加载音频...); drawnow; try % 读取音频 (兼容旧版 audioread) [audioData, fs] audioread(fullPath); % 如果是立体声转为单声道 if size(audioData, 2) 1 audioData mean(audioData, 2); end % 绘制时域图 t (0:length(audioData)-1) / fs; plot(axTime, t, audioData, b, LineWidth, 0.8); xlim(axTime, [0, t(end)]); title(axTime, sprintf(时域波形 (%.2f s), t(end))); % 初步绘制频谱 (FFT) N length(audioData); Y fft(audioData); P2 abs(Y/N); P1 P2(1:floor(N/2)1); P1(2:end-1) 2*P1(2:end-1); f fs*(0:(N/2))/N; plot(axFreq, f, P1, r, LineWidth, 0.8); xlim(axFreq, [0, fs/2]); title(axFreq, 幅度频谱 (FFT)); xlabel(axFreq, 频率 (Hz)); ylabel(axFreq, 幅值); set(hStatus, String, 音频加载成功 | 点击开始识别进行分析); set(hResultGenre, String, --); set(hResultConf, String, 置信度: --%); catch ME errordlg([读取文件失败: , ME.message], 错误); set(hStatus, String, 错误无法加载音频); end end function playCallback(~, ~) if isempty(audioData) warndlg(请先导入音乐文件, 提示); return; end set(hStatus, String, 正在播放...); sound(audioData, fs); end function pauseCallback(~, ~) % MATLAB 原生 sound 函数不支持直接暂停这里仅做提示或停止 % 若要实现精确暂停需要使用 audioplayer 对象此处简化处理为停止 stopCallback(~, ~); set(hStatus, String, 播放已停止 (MATLAB 原生 sound 不支持暂停)); end function stopCallback(~, ~) sound([], fs); % 停止发声 set(hStatus, String, 播放已停止); end function recognizeCallback(~, ~) if isempty(audioData) warndlg(请先导入音乐文件, 提示); return; end set(hStatus, String, 正在提取 MFCC 特征并运行 CNN 模型...); drawnow; % --- 步骤 1: MFCC 特征提取 (核心算法) --- % 注意如果没有 Audio Toolbox这里使用简化的手动 MFCC 计算逻辑 % 如果有 Audio Toolbox可直接调用 mfcc(audioData, fs) try % 尝试使用工具箱 if exist(mfcc, file) 2 features mfcc(audioData, fs); else % 手动简化版 MFCC 计算 (用于无工具箱环境演示) features compute_manual_mfcc(audioData, fs); end % 可视化 MFCC (热力图) imagesc(axFreq, features); axis xy; title(axFreq, MFCC 特征图谱 (Mel-Frequency Cepstral Coefficients)); xlabel(axFreq, 帧数); ylabel(axFreq, MFCC 系数); colorbar(axFreq); % --- 步骤 2: CNN 推理 --- % 在实际系统中这里应该加载训练好的网络: net load(myCNNNet.mat); % 然后进行 predict: label classify(net, features); % 【演示模式】由于无法在此提供训练好的 .mat 文件 % 我们模拟一个基于特征能量的简单分类逻辑或者随机选择一个结果作为演示 % 真实项目中请替换为真实的深度学习预测代码 pause(1.5); % 模拟计算延迟 % 模拟识别结果 (实际请替换为真实模型输出) % 这里为了演示效果根据文件名或随机给出一个结果 predictedIdx randi(length(model.classes)); genre model.classes{predictedIdx}; confidence 75 rand() * 20; % 模拟 75%-95% 置信度 % 更新界面 set(hResultGenre, String, genre); set(hResultConf, String, sprintf(置信度: %.2f%%, confidence)); set(hStatus, String, [识别完成 | 结果, genre]); % 高亮显示 set(hResultGenre, ForegroundColor, [0, 0.6, 0]); catch ME errordlg([识别过程出错: , ME.message], 错误); set(hStatus, String, 识别失败); end end % --- 辅助函数手动计算简易 MFCC (当没有 Audio Toolbox 时) --- function coeffs compute_manual_mfcc(signal, fs) % 这是一个极度简化的 MFCC 模拟仅用于演示流程 % 真实 MFCC 需要预加重 - 分帧 - 加窗 - FFT - Mel 滤波器组 - 对数 - DCT frameSize 256; hopSize 128; numFrames floor((length(signal) - frameSize) / hopSize) 1; numCoeffs 13; coeffs zeros(numCoeffs, numFrames); % 简单的汉明窗 window hamming(frameSize); for i 1:numFrames startIdx (i-1)*hopSize 1; frame signal(startIdx : startIdx frameSize - 1) .* window; % FFT spec abs(fft(frame)); spec spec(1:frameSize/21); % 模拟 Mel 滤波和对数 (简化为对低频部分采样) logSpec log(spec(1:min(40, length(spec))) 1e-10); % 模拟 DCT (取前13个系数) if length(logSpec) numCoeffs % 简单的离散余弦变换模拟 for k 1:numCoeffs coeffs(k, i) sum(logSpec .* cos(pk(0:length(logSpec)-1)/length(logSpec))); end else coeffs(:, i) logSpec(1:min(numCoeffs, length(logSpec))); end end endend如何部署真实模型 (进阶指南)准备数据确保你拥有 GTZAN 数据集文件夹结构如下GTZAN_Dataset/├── blues/├── classical/├── country/… (共10个文件夹)% train_cnn_music.m (示例逻辑需单独运行)% 1. 读取所有音频提取 MFCC% 2. 将 MFCC 调整为统一尺寸 (例如 13x40)作为图像输入% 3. 构建 CNN 网络layers [imageInputLayer([13 40 1])convolution2dLayer(3, 16, ‘Padding’, ‘same’)batchNormalizationLayerreluLayermaxPooling2dLayer(2, ‘Stride’, 2)fullyConnectedLayer(10)softmaxLayerclassificationLayer];% 4. 训练网络 (trainingOptions, trainNetwork)% 5. save(‘trained_model.mat’, ‘net’);修改 GUI 代码连接模型在上面的 GUI 代码 recognizeCallback 函数中将【演示模式】部分替换为% — 真实模型加载与预测 —if ~exist(‘trained_net’, ‘var’)loaded load(‘trained_model.mat’); % 加载你训练好的模型trained_net loaded.net;end% 预处理特征以匹配网络输入inputSize trained_net.Layers(1).InputSize;% resize features to inputSize…% 预测[labelIdx, scores] classify(trained_net, processedFeatures);genre trained_net.Classes(labelIdx);confidence max(scores) * 100;系统功能亮点总结双图联动左侧控制右侧同时展示时域波形看节奏和 MFCC 热力图看音色纹理这是音乐流派识别最直观的特征。模块化设计特征提取 (compute_manual_mfcc 或 mfcc) 与 分类逻辑分离方便替换更先进的模型如 LSTM, ResNet。容错性即使没有安装昂贵的 Audio Toolbox代码也能通过内置的简化算法运行保证界面和流程演示不报错。交互友好清晰的按钮状态、实时的状态栏提示、大字号的结果显示。在这里插入图片描述标题区粉色大字“音乐流派识别检测处理系统”左上图时域波形图时间 vs 振幅右上图频域波形图频率 vs 幅度左下控制面板“音乐管理”组 —— 选择音乐、录音5秒、播放音乐右下控制面板“功能菜单”组 —— 停止播放、生成时域图、退出系统、开始识别、生成频谱图、全部清空底部信息栏左侧“识别信息”组 —— “音乐已加载”、“音乐流派爵士”右侧“音乐路径”文本框显示当前文件完整路径 核心功能假设使用 audioread 读取音频时域图 原始信号波形频域图 FFT 幅度谱“开始识别” → 模拟返回“爵士”可替换为真实模型“录音5秒” → 调用 audiorecorder 录制5秒所有按钮均有回调函数即可运行function MusicGenreSystem()% 音乐流派识别检测处理系统 GUI% 匹配截图界面布局与功能%% 1. 创建主窗口 hFig figure(Name, 音乐流派识别检测处理系统, ... NumberTitle, off, ... Position, [100, 100, 900, 650], ... MenuBar, none, ... ToolBar, none, ... Resize, off, ... Color, [0.98, 0.98, 0.98]); %% 2. 标题 uicontrol(Style, text, String, 音乐流派识别检测处理系统, ... FontSize, 20, FontWeight, bold, ForegroundColor, [0.8, 0.2, 0.6], ... Position, [300, 600, 300, 40], ... HorizontalAlignment, center, BackgroundColor, [0.98, 0.98, 0.98]); %% 3. 时域波形图 (左上) axTime axes(Parent, hFig, Position, [0.1, 0.55, 0.38, 0.35]); title(axTime, 时域波形, FontSize, 10); xlabel(axTime, 时间 (秒)); ylabel(axTime, 振幅); grid on; box on; xlim(axTime, [0 35]); ylim(axTime, [-0.6 0.8]); %% 4. 频域波形图 (右上) axFreq axes(Parent, hFig, Position, [0.52, 0.55, 0.38, 0.35]); title(axFreq, 频域波形, FontSize, 10); xlabel(axFreq, 频率 (Hz)); ylabel(axFreq, 幅度); grid on; box on; xlim(axFreq, [0 12000]); ylim(axFreq, [0 1400]); %% 5. 音乐管理面板 (左下) panelMusic uipanel(Parent, hFig, Title, 音乐管理, ... Position, [20, 280, 260, 180], ... ForegroundColor, [0.2, 0.6, 0.2], ... FontWeight, bold); btnSelect uicontrol(Parent, panelMusic, Style, pushbutton, String, 选择音乐, ... Position, [20, 120, 70, 30], Callback, selectMusicCallback); btnRecord uicontrol(Parent, panelMusic, Style, pushbutton, String, 录音5秒, ... Position, [110, 120, 70, 30], Callback, recordMusicCallback); btnPlay uicontrol(Parent, panelMusic, Style, pushbutton, String, 播放音乐, ... Position, [200, 120, 70, 30], Callback, playMusicCallback); %% 6. 功能菜单面板 (右下) panelFunc uipanel(Parent, hFig, Title, 功能菜单, ... Position, [480, 280, 260, 180], ... ForegroundColor, [0.2, 0.6, 0.2], ... FontWeight, bold); btnStop uicontrol(Parent, panelFunc, Style, pushbutton, String, 停止播放, ... Position, [20, 120, 70, 30], Callback, stopPlaybackCallback); btnGenTime uicontrol(Parent, panelFunc, Style, pushbutton, String, 生成时域图, ... Position, [110, 120, 70, 30], Callback, genTimePlotCallback); btnExit uicontrol(Parent, panelFunc, Style, pushbutton, String, 退出系统, ... Position, [200, 120, 70, 30], Callback, (~,~) close(hFig)); btnRecognize uicontrol(Parent, panelFunc, Style, pushbutton, String, 开始识别, ... Position, [20, 60, 70, 30], Callback, recognizeCallback); btnGenSpec uicontrol(Parent, panelFunc, Style, pushbutton, String, 生成频谱图, ... Position, [110, 60, 70, 30], Callback, genSpecPlotCallback); btnClearAll uicontrol(Parent, panelFunc, Style, pushbutton, String, 全部清空, ... Position, [200, 60, 70, 30], Callback, clearAllCallback); %% 7. 识别信息面板 (左下底部) panelInfo uipanel(Parent, hFig, Title, 识别信息, ... Position, [20, 100, 260, 160], ... ForegroundColor, [0.8, 0.2, 0.8], ... FontWeight, bold); uicontrol(Parent, panelInfo, Style, text, String, 音乐已加载, ... Position, [20, 80, 100, 30], FontSize, 12, HorizontalAlignment, center); hResult uicontrol(Parent, panelInfo, Style, text, String, 音乐流派爵士, ... Position, [140, 80, 100, 30], FontSize, 12, ForegroundColor, [0.8, 0.2, 0.2], ... HorizontalAlignment, center); %% 8. 音乐路径显示 (右下底部) panelPath uipanel(Parent, hFig, Title, 音乐路径, ... Position, [480, 100, 400, 160], ... ForegroundColor, [0.8, 0.2, 0.2], ... FontWeight, bold); hPathText uicontrol(Parent, panelPath, Style, edit, String, , ... Position, [20, 60, 360, 80], Max, 2, Min, 0, ... Enable, inactive, FontSize, 9); %% 9. 全局变量存储 persistent audioData fs recorder isPlaying; if isempty(audioData), audioData []; end if isempty(fs), fs []; end if isempty(recorder), recorder []; end if isempty(isPlaying), isPlaying false; end %% 回调函数定义 function selectMusicCallback(~, ~) [file, path] uigetfile({.wav;.mp3.flac;.aiff, Audio Files; ., All Files}, 选择音乐文件); if isequal(file, 0) return; end fullPath fullfile(path, file); set(hPathText, String, fullPath); try [audioData, fs] audioread(fullPath); if size(audioData, 2) 1 audioData mean(audioData, 2); % 转单声道 end % 自动绘制时域和频域图 genTimePlotCallback(~, ~); genSpecPlotCallback(~, ~); set(hResult, String, 音乐流派--); msgbox(音乐已成功加载, 提示, modal); catch ME errordlg([加载失败: , ME.message], 错误); end end function recordMusicCallback(~, ~) if ~isempty(recorder) recorder.Running stop(recorder); end recorder audiorecorder(fs); % 默认采样率 record(recorder); msgbox(正在录音... 5秒后自动停止, 录音中, modal); timerObj timer(StartDelay, 5, TimerFcn, (~,~) stopRecording()); start(timerObj); function stopRecording() stop(recorder); audioData getaudiodata(recorder); fs recorder.SampleRate; if size(audioData, 2) 1 audioData mean(audioData, 2); end % 更新路径显示 set(hPathText, String, 录制的音频 (5秒)); % 绘图 genTimePlotCallback(~, ~); genSpecPlotCallback(~, ~); set(hResult, String, 音乐流派--); msgbox(录音完成, 成功, modal); end end function playMusicCallback(~, ~) if isempty(audioData) warndlg(请先选择或录制音乐, 提示); return; end if isPlaying sound([], fs); % 停止当前播放 end isPlaying true; sound(audioData, fs); % 播放结束后重置标志简化处理 timerObj timer(StartDelay, length(audioData)/fs, TimerFcn, (~,~) set(isPlaying, false)); start(timerObj); end function stopPlaybackCallback(~, ~) sound([], fs); isPlaying false; msgbox(播放已停止, 提示, modal); end function genTimePlotCallback(~, ~) if isempty(audioData) warndlg(无音频数据, 提示); return; end t (0:length(audioData)-1) / fs; plot(axTime, t, audioData, b, LineWidth, 0.8); xlim(axTime, [0 max(t)]); ylim(axTime, [min(audioData1.1 max(audioData)1.1]); title(axTime, 时域波形); xlabel(axTime, 时间 (秒)); ylabel(axTime, 振幅); grid on; box on; end function genSpecPlotCallback(~, ~) if isempty(audioData) warndlg(无音频数据, 提示); return; end N length(audioData); Y fft(audioData); P2 abs(Y/N); P1 P2(1:floor(N/2)1); P1(2:end-1) 2*P1(2:end-1); f fs*(0:(N/2))/N; plot(axFreq, f, P1, r, LineWidth, 0.8); xlim(axFreq, [0 min(12000, fs/2)]); ylim(axFreq, [0 max(P1)*1.1]); title(axFreq, 频域波形); xlabel(axFreq, 频率 (Hz)); ylabel(axFreq, 幅度); grid on; box on; end function recognizeCallback(~, ~) if isempty(audioData) warndlg(请先加载音乐, 提示); return; end set(hResult, String, 识别中...); drawnow; % 模拟识别延迟 pause(1.5); % 模拟识别结果实际应调用 trained model genres {爵士, 摇滚, 流行, 古典, 电子, 嘻哈, 乡村, 蓝调, 金属, 雷鬼}; predictedGenre genres{randi(length(genres))}; set(hResult, String, [音乐流派, predictedGenre]); msgbox([识别完成n流派, predictedGenre], 识别结果, modal); end function clearAllCallback(~, ~) cla(axTime); cla(axFreq); set(hPathText, String, ); set(hResult, String, 音乐流派--); audioData []; fs []; if ~isempty(recorder) recorder.Running stop(recorder); end recorder []; isPlaying false; msgbox(所有数据已清空, 提示, modal); endend 界面还原说明截图元素 代码对应部分粉色标题 uicontrol(… ‘ForegroundColor’, [0.8, 0.2, 0.6])时域/频域图 axes plot“音乐管理”按钮组 uipanel 三个 pushbutton“功能菜单”按钮组 uipanel 六个 pushbutton“识别信息”显示 两个 text 控件“音乐路径”文本框 edit 控件 inactive 状态