用MATLAB实现变声器处理

function varargout = shengyinshiyan(varargin)
% SHENGYINSHIYAN MATLAB code for shengyinshiyan.fig
%      SHENGYINSHIYAN, by itself, creates a new SHENGYINSHIYAN or raises the existing
%      singleton*.
%
%      H = SHENGYINSHIYAN returns the handle to a new SHENGYINSHIYAN or the handle to
%      the existing singleton*.
%
%      SHENGYINSHIYAN('CALLBACK',hObject,eventData,handles,...) calls the local
%      function named CALLBACK in SHENGYINSHIYAN.M with the given input arguments.
%
%      SHENGYINSHIYAN('Property','Value',...) creates a new SHENGYINSHIYAN or raises the
%      existing singleton*.  Starting from the left, property value pairs are
%      applied to the GUI before shengyinshiyan_OpeningFcn gets called.  An
%      unrecognized property name or invalid value makes property application
%      stop.  All inputs are passed to shengyinshiyan_OpeningFcn via varargin.
%
%      *See GUI Options on GUIDE's Tools menu.  Choose "GUI allows only one
%      instance to run (singleton)".
%
% See also: GUIDE, GUIDATA, GUIHANDLES

% Edit the above text to modify the response to help shengyinshiyan

% Last Modified by GUIDE v2.5 28-Dec-2023 15:45:48

% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @shengyinshiyan_OpeningFcn, ...
                   'gui_OutputFcn',  @shengyinshiyan_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
% End initialization code - DO NOT EDIT


% --- Executes just before shengyinshiyan is made visible.
function shengyinshiyan_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
% varargin   command line arguments to shengyinshiyan (see VARARGIN)

% Choose default command line output for shengyinshiyan
handles.output = hObject;

% Update handles structure
guidata(hObject, handles);

% UIWAIT makes shengyinshiyan wait for user response (see UIRESUME)
% uiwait(handles.figure1);


% --- Outputs from this function are returned to the command line.
function varargout = shengyinshiyan_OutputFcn(hObject, eventdata, handles) 
% varargout  cell array for returning output args (see VARARGOUT);
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Get default command line output from handles structure
varargout{1} = handles.output;


% --- Executes on button press in pushbutton1.
function pushbutton1_Callback(hObject, eventdata, handles)
warning off  %取消警告
feature  jit off %加速通道 

fs = 44100;  % 采样率(以赫兹为单位)
duration = 15;  % 录音时长(以秒为单位)

recObj = audiorecorder(fs, 16, 1);  % 创建音频录制器对象
disp('开始录音,请讲话...');  % 提示用户开始录音
recordblocking(recObj, duration);  % 录制音频
disp('录音结束。');

audioData = getaudiodata(recObj);  % 获取录制的音频数据

filename = 'liuzhanpeng_audio.wav';  % 保存音频的文件名
audiowrite(filename, audioData, fs);  % 将音频数据保存为.wav文件
disp(['音频已保存为 ' filename]);

% --- Executes on button press in pushbutton2.v
function pushbutton2_Callback(hObject, eventdata, handles)
global  noise
[y,fs]=audioread('liuzhanpeng_audio.wav'); %读取声音文件
sound(y,fs);
N=length(y); %长度
n=0:1:N-1;
t=n/fs;
w=2*n*pi/N;
f=(0:N-1)*fs/N;
y1=fft(y); %对原始信号做FFT变换

noise=0.05*randn(1,N);
y_with_noise=y + noise';

figure;
subplot(2,1,1); %做原始语音信号的频谱图
plot(t,y) %做原始语音信号的时域波形图
title('语音信号时域图');
xlabel('时间t');
ylabel('幅值');
subplot(2,1,2); %做原始语音信号的频谱图
axis([0,5000,0,800]);
plot(f,abs(y1));
title('语音信号频谱')
xlabel('频率/HZ');
ylabel('幅度');
disp('播放完毕。');

% --- Executes on button press in pushbutton3.
function pushbutton3_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton3 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
% 读取语音文件
global  noise
global  y_with_noise
[y, Fs] = audioread('liuzhanpeng_audio.wav'); % 替换为你的语音文件路径

y_with_noise=y + noise';
% 保存加入噪声后的语音文件
audiowrite('liuzhanpeng_audio1.wav', y_with_noise, Fs); % 替换为你想保存的文件路径

% 计算FFT
L = length(y_with_noise);
Y = fft(y_with_noise);
f = Fs*(0:(L/2))/L;
P = abs(Y/L);

figure;
% 绘制加入噪声后的语音时域图像
subplot(2, 1, 1);
plot((0:length(y_with_noise)-1)/Fs, y_with_noise);
xlabel('时间 (秒)');
ylabel('幅度');
title('加入噪声后的语音时域图像');
subplot(2, 1,2);
% 绘制加入噪声后的语音频域图像
plot(f, 2*P(1:L/2+1));
title('加入高斯白噪声后的频域图像');
xlabel('频率 (Hz)');
ylabel('幅度');

% 播放加入噪声后的语音
sound(y_with_noise, Fs);
% 显示图形
grid on;

disp('播放完毕。');




% --- Executes on button press in pushbutton4.
function pushbutton4_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton4 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
global  lvboy
% % 读取语音文件
[y, Fs] = audioread('liuzhanpeng_audio1.wav'); % 替换为你的语音文件路径

sound(lvboy, Fs);
N=length(y); 
f=(0:N-1)*Fs/N;
% % 保存滤波后的语音文件
% audiowrite('l_audio2.wav',lvboy, Fs); % 替换为你想保存的文件路径
figure;
% 绘制加入噪声后的语音时域图像
subplot(2, 1, 1);
plot((0:length(lvboy)-1)/Fs,lvboy);
xlabel('时间 (秒)');
ylabel('幅度');
title('滤波后的时域图像');

% 绘制原始语音频域图像

subplot(2, 1,2);
plot(f,abs(fft(lvboy)));
xlabel('频率 (Hz)');
ylabel('幅度');
title('滤波后的频域图像');








% --- Executes on button press in pushbutton5.
function pushbutton5_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton5 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
warning off
feature jit off
clc,clear,close all


% --- Executes on button press in pushbutton7.
function pushbutton7_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton7 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
global  noise
[y, Fs] = audioread('liuzhanpeng_audio.wav'); % 替换为你的语音文件路径
N=length(y); 
f=(0:N-1)*Fs/N;
Y = fft(noise);
figure;
% 绘制加入噪声后的语音时域图像
subplot(2, 1, 1);
plot((0:length(noise)-1)/Fs,noise);
xlabel('时间 (秒)');
ylabel('幅度');
title('生成噪声的时域图像');
subplot(2, 1,2);
% 绘制加入噪声后的语音频域图像
plot(f, abs(Y));
title('生成噪声的频域图像');
xlabel('频率 (Hz)');
ylabel('幅度');


% --- Executes on button press in pushbutton8.
function pushbutton8_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton8 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
global  y_with_noise
global  lvboy
fp=800;fs=1300;rs=35;rp=0.5;Fs=44100;
wp=2*Fs*tan(2*pi*fp/(2*Fs));
ws=2*Fs*tan(2*pi*fs/(2*Fs));
[n,wn]=buttord(wp,ws,rp,rs,'s');
[b,a]=butter(n,wn,'s');
[num,den]=bilinear(b,a,Fs);
[h,w]=freqz(num,den,512,Fs);
figure;
plot(w,abs(h));
xlabel('频率/Hz');ylabel('幅值');
title('巴特沃斯低通滤波器幅度特性');
lvboy=filter(num,den,y_with_noise);%滤波后的波形


% --- Executes on button press in pushbutton9.
function pushbutton9_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton9 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

[x,Fs] = audioread('liuzhanpeng_audio.wav');
 N=length(x); 
f=(0:N-1)*Fs/N;
x = x(:,1);
n = 14;    
%参数:男声变女声n>1
%女声变男声n<1
 
y = shiftPitch(x,n);
 
sound(y,Fs);

figure;
% 绘制加入噪声后的语音时域图像
subplot(2, 1, 1);
plot(f,abs(fft(x)));
xlabel('频率 (Hz)');
ylabel('幅度');
title('男声的频域图像');

% 绘制原始语音频域图像

subplot(2, 1,2);
plot(f,abs(fft(y)));
xlabel('频率 (Hz)');
ylabel('幅度 ');
title('女声的频域图像');

figure;
subplot(2, 1, 1);
plot((0:length(x)-1)/Fs,x);
xlabel('时间 (秒)');
ylabel('幅度');
title('男声的时域图像');

subplot(2, 1, 2);
plot((0:length(y)-1)/Fs,y);
xlabel('时间 (秒)');
ylabel('幅度');
title('女声的时域图像');







  • 5
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Liuzp36

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值