声振论坛

 找回密码
 我要加入

QQ登录

只需一步,快速开始

查看: 6414|回复: 6

[综合讨论] 请问matlab改变语音信号频率(改变男女声)?

[复制链接]
发表于 2012-12-20 15:17 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?我要加入

x




[s,Fs,bits]=wavread('D:\yuyin\lushang.wav');
sound(s,Fs,bits)  %(回放)
y=s(:,2); %处理立体声的一个声道
sound(y,2*Fs,bits) %改变播放速度 a=fft(y,n);%傅里叶变换。请问matlab改变语音信号频率(改变男女声)? 听说频谱频域可以,具体代码是?


回复
分享到:

使用道具 举报

发表于 2012-12-24 14:27 | 显示全部楼层
可以参考下面的程序

  1. function varargout = boy_girl(varargin)
  2. % BOY_GIRL M-file for boy_girl.fig
  3. %      BOY_GIRL, by itself, creates a new BOY_GIRL or raises the existing
  4. %      singleton*.
  5. %
  6. %      H = BOY_GIRL returns the handle to a new BOY_GIRL or the handle to
  7. %      the existing singleton*.
  8. %
  9. %      BOY_GIRL('CALLBACK',hObject,eventData,handles,...) calls the local
  10. %      function named CALLBACK in BOY_GIRL.M with the given input arguments.
  11. %
  12. %      BOY_GIRL('Property','Value',...) creates a new BOY_GIRL or raises the
  13. %      existing singleton*.  Starting from the left, property value pairs are
  14. %      applied to the GUI before boy_girl_OpeningFcn gets called.  An
  15. %      unrecognized property name or invalid value makes property application
  16. %      stop.  All inputs are passed to boy_girl_OpeningFcn via varargin.
  17. %
  18. %      *See GUI Options on GUIDE's Tools menu.  Choose "GUI allows only one
  19. %      instance to run (singleton)".
  20. %
  21. % See also: GUIDE, GUIDATA, GUIHANDLES

  22. % Edit the above text to modify the response to help boy_girl

  23. % Last Modified by GUIDE v2.5 07-May-2011 12:11:56

  24. % Begin initialization code - DO NOT EDIT
  25. gui_Singleton = 1;
  26. gui_State = struct('gui_Name',       mfilename, ...
  27.                    'gui_Singleton',  gui_Singleton, ...
  28.                    'gui_OpeningFcn', @boy_girl_OpeningFcn, ...
  29.                    'gui_OutputFcn',  @boy_girl_OutputFcn, ...
  30.                    'gui_LayoutFcn',  [] , ...
  31.                    'gui_Callback',   []);
  32. if nargin && ischar(varargin{1})
  33.     gui_State.gui_Callback = str2func(varargin{1});
  34. end

  35. if nargout
  36.     [varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
  37. else
  38.     gui_mainfcn(gui_State, varargin{:});
  39. end
  40. % End initialization code - DO NOT EDIT


  41. % --- Executes just before boy_girl is made visible.
  42. function boy_girl_OpeningFcn(hObject, ~, handles, varargin)
  43. % This function has no output args, see OutputFcn.
  44. % hObject    handle to figure
  45. % eventdata  reserved - to be defined in a future version of MATLAB
  46. % handles    structure with handles and user data (see GUIDATA)
  47. % varargin   command line arguments to boy_girl (see VARARGIN)

  48. % Choose default command line output for boy_girl
  49. handles.output = hObject;

  50. % Update handles structure
  51. guidata(hObject, handles);

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


  54. % --- Outputs from this function are returned to the command line.
  55. function varargout = boy_girl_OutputFcn(~, ~, handles)
  56. % varargout  cell array for returning output args (see VARARGOUT);
  57. % hObject    handle to figure
  58. % eventdata  reserved - to be defined in a future version of MATLAB
  59. % handles    structure with handles and user data (see GUIDATA)
  60. set(handles.time_edit,'string','10');
  61. set(handles.freq_edit,'string','8000');
  62. % Get default command line output from handles structureoriginal
  63. varargout{1} = handles.output;



  64. function time_edit_Callback(~, ~, ~)
  65. % hObject    handle to time_edit (see GCBO)
  66. % eventdata  reserved - to be defined in a future version of MATLAB
  67. % handles    structure with handles and user data (see GUIDATA)

  68. % Hints: get(hObject,'String') returns contents of time_edit as text
  69. %        str2double(get(hObject,'String')) returns contents of time_edit as a double


  70. % --- Executes during object creation, after setting all properties.
  71. function time_edit_CreateFcn(hObject, ~, ~)
  72. % hObject    handle to time_edit (see GCBO)
  73. % eventdata  reserved - to be defined in a future version of MATLAB
  74. % handles    empty - handles not created until after all CreateFcns called

  75. % Hint: edit controls usually have a white background on Windows.
  76. %       See ISPC and COMPUTER.
  77. if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
  78.     set(hObject,'BackgroundColor','white');
  79. end



  80. function freq_edit_Callback(~, eventdata, handles)
  81. % hObject    handle to freq_edit (see GCBO)
  82. % eventdata  reserved - to be defined in a future version of MATLAB
  83. % handles    structure with handles and user data (see GUIDATA)

  84. % Hints: get(hObject,'String') returns contents of freq_edit as text
  85. %        str2double(get(hObject,'String')) returns contents of freq_edit as a double


  86. % --- Executes during object creation, after setting all properties.
  87. function freq_edit_CreateFcn(hObject, ~, ~)
  88. % hObject    handle to freq_edit (see GCBO)
  89. % eventdata  reserved - to be defined in a future version of MATLAB
  90. % handles    empty - handles not created until after all CreateFcns called

  91. % Hint: edit controls usually have a white background on Windows.
  92. %       See ISPC and COMPUTER.
  93. if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
  94.     set(hObject,'BackgroundColor','white');
  95. end


  96. % --- Executes on button press in start_pushbutton.
  97. function start_pushbutton_Callback(hObject, ~, handles)
  98. % hObject    handle to start_pushbutton (see GCBO)
  99. % eventdata  reserved - to be defined in a future version of MATLAB
  100. % handles    structure with handles and user data (see GUIDATA)
  101. freq=str2num(get(handles.freq_edit,'String'));
  102. time=str2num(get(handles.time_edit,'string'));
  103. ai=analoginput('winsound',0);%初始化录音麦克
  104. chanel=addchannel(ai,1);%1表示单声道
  105. set(ai,'SampleRate',freq);
  106. T=time; %录音时间
  107. set(ai,'SamplesPerTrigger',T*freq);
  108. start(ai);
  109. y=0;time=0;
  110. [y,time]=getdata(ai);%读出相应的数据
  111. handles.y=y;
  112. handles.Fs=freq;
  113. handles.yoriginal=y;
  114. handles.foriginal=freq;
  115. guidata(hObject,handles);
  116. plot(handles.axes1,time,handles.y)%画出声音波形
  117. title(handles.axes1,'时域图');
  118. xlabel(handles.axes1,'时间');
  119. ylabel(handles.axes1,'频率');
  120. set(handles.axes1,'Xgrid','on');
  121. set(handles.axes1,'Ygrid','on');


  122. % --- Executes on selection change in xuanpin_popupmenu.
  123. function xuanpin_popupmenu_Callback(hObject, eventdata, handles)
  124. % hObject    handle to xuanpin_popupmenu (see GCBO)
  125. % eventdata  reserved - to be defined in a future version of MATLAB
  126. % handles    structure with handles and user data (see GUIDATA)
  127. contents = cellstr(get(handles.xuanpin_popupmenu,'String')) ;%获取listbox1中的细胞矩阵
  128. item=contents{get(handles.xuanpin_popupmenu,'Value')};%获取listbox中的值
  129. if strcmp(item,'自定义')
  130.     prompt={'频率编辑'};

  131. title='请输入频率:';

  132. lines=1;

  133. def={'8000'};

  134. answer=inputdlg(prompt,title,lines,def);
  135. handles.Fs=str2num(answer{1});
  136. guidata(hObject,handles);
  137. else
  138. end
  139. % Hints: contents = cellstr(get(hObject,'String')) returns xuanpin_popupmenu contents as cell array
  140. %        contents{get(hObject,'Value')} returns selected item from xuanpin_popupmenu


  141. % --- Executes during object creation, after setting all properties.
  142. function xuanpin_popupmenu_CreateFcn(hObject, eventdata, handles)
  143. % hObject    handle to xuanpin_popupmenu (see GCBO)
  144. % eventdata  reserved - to be defined in a future version of MATLAB
  145. % handles    empty - handles not created until after all CreateFcns called

  146. % Hint: popupmenu controls usually have a white background on Windows.
  147. %       See ISPC and COMPUTER.
  148. if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
  149.     set(hObject,'BackgroundColor','white');
  150. end


  151. % --- Executes on button press in open_pushbutton.
  152. function open_pushbutton_Callback(hObject, eventdata, handles)
  153. % hObject    handle to open_pushbutton (see GCBO)
  154. % eventdata  reserved - to be defined in a future version of MATLAB
  155. % handles    structure with handles and user data (see GUIDATA)
  156. [filename pathname]=uigetfile({'*.wav','ALL FILES(*.*)'},'选择声音文件');
  157. if isequal([filename pathname],[0,0])
  158.     return;
  159. end
  160. strname=[pathname filename];%选择的声音文件路径和文件名
  161. [data Fs]=wavread(strname);%temp表示声音数据 Fs表示频率
  162. handles.y=data;
  163. handles.Fs=Fs;
  164. handles.yoriginal=data;
  165. handles.foriginal=Fs;
  166. guidata(hObject,handles);



  167. % --- Executes on button press in play_pushbutton.
  168. function play_pushbutton_Callback(hObject, eventdata, handles)
  169. % hObject    handle to play_pushbutton (see GCBO)
  170. % eventdata  reserved - to be defined in a future version of MATLAB
  171. % handles    structure with handles and user data (see GUIDATA)
  172. c=handles.Fs;
  173. contents = cellstr(get(handles.xuanpin_popupmenu,'String')) ;%获取listbox1中的细胞矩阵
  174. item=contents{get(handles.xuanpin_popupmenu,'Value')};%获取listbox中的值
  175. if strcmp(item,'默认频率')
  176.     Fs=c;
  177. elseif strcmp(item,'低频')
  178.     Fs=0.7*c;
  179. elseif strcmp(item,'高频')
  180.     Fs=1.3*c;
  181. else
  182.    Fs=handles.Fs;
  183. end;
  184.     wavplay(handles.y,Fs);
  185.     N=length(handles.y);
  186.     y1=fft(handles.y,N);
  187.     f1=0:Fs/N:Fs*(N-1)/N;
  188.     plot(handles.axes1,f1,handles.y);
  189.     title(handles.axes1,'原始时域图');
  190.     xlabel(handles.axes1,'时间');
  191.     ylabel(handles.axes1,'频率');
  192.     set(handles.axes1,'Xgrid','on');
  193.     set(handles.axes1,'Ygrid','on');
  194.    
  195.    
  196.     plot(handles.axes2,f1(1:N/2),y1(1:N/2));
  197.     set(handles.axes2,'Xgrid','on');
  198.     set(handles.axes2,'Ygrid','on');
  199.     title(handles.axes2,'频谱图');
  200.     xlabel( handles.axes2,'频率');
  201.     ylabel( handles.axes2,'幅度');
  202.    

  203. % --- Executes on button press in yuchuli2_radiobutton.
  204. function yuchuli2_radiobutton_Callback(hObject, eventdata, handles)
  205. % hObject    handle to yuchuli2_radiobutton (see GCBO)
  206. % eventdata  reserved - to be defined in a future version of MATLAB
  207. % handles    structure with handles and user data (see GUIDATA)

  208. % Hint: get(hObject,'Value') returns toggle state of yuchuli2_radiobutton


  209. % --- Executes on button press in yuchuli1_radiobutton.
  210. function yuchuli1_radiobutton_Callback(hObject, eventdata, handles)
  211. % hObject    handle to yuchuli1_radiobutton (see GCBO)
  212. % eventdata  reserved - to be defined in a future version of MATLAB
  213. % handles    structure with handles and user data (see GUIDATA)
  214.    
  215. % Hint: get(hObject,'Value') returns toggle state of yuchuli1_radiobutton

  216. % --- Executes on selection change in yuchuli3_popupmenu.
  217. function yuchuli3_popupmenu_Callback(hObject, eventdata, handles)
  218. % hObject    handle to yuchuli3_popupmenu (see GCBO)
  219. % eventdata  reserved - to be defined in a future version of MATLAB
  220. % handles    structure with handles and user data (see GUIDATA)
  221. contents = cellstr(get(handles.xuanpin_popupmenu,'String')) ;%获取listbox1中的细胞矩阵
  222. item=contents{get(handles.xuanpin_popupmenu,'Value')};%获取listbox中的值
  223. if strcmp(item,'低通滤波')
  224. prompt={'请输入滤波上限频率:'};

  225. title='请输入滤波上限频率:';

  226. lines=1;

  227. def={'2000'};

  228. answer=inputdlg(prompt,title,lines,def);
  229. handles.limit=str2num(answer{1});
  230. else
  231. prompt={'请输入滤波下限频率:'};

  232. title='请输入滤波下限频率:';

  233. lines=1;

  234. def={'1000'};

  235. answer=inputdlg(prompt,title,lines,def);
  236. handles.limit=str2num(answer{1});   
  237. end
  238. guidata(hObject,handles);

  239. % Hints: contents = cellstr(get(hObject,'String')) returns yuchuli3_popupmenu contents as cell array
  240. %        contents{get(hObject,'Value')} returns selected item from yuchuli3_popupmenu


  241. % --- Executes during object creation, after setting all properties.
  242. function yuchuli3_popupmenu_CreateFcn(hObject, eventdata, handles)
  243. % hObject    handle to yuchuli3_popupmenu (see GCBO)
  244. % eventdata  reserved - to be defined in a future version of MATLAB
  245. % handles    empty - handles not created until after all CreateFcns called

  246. % Hint: popupmenu controls usually have a white background on Windows.
  247. %       See ISPC and COMPUTER.
  248. if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
  249.     set(hObject,'BackgroundColor','white');
  250. end


  251. % --- Executes on button press in chuli_pushbutton.
  252. function chuli_pushbutton_Callback(hObject, eventdata, handles)
  253. % hObject    handle to chuli_pushbutton (see GCBO)
  254. % eventdata  reserved - to be defined in a future version of MATLAB
  255. % handles    structure with handles and user data (see GUIDATA)
  256. contents = cellstr(get(handles.xuanpin_popupmenu,'String')) ;%获取listbox1中的细胞矩阵
  257. item=contents{get(handles.xuanpin_popupmenu,'Value')};%获取listbox中的值
  258. v1=get(handles.yuchuli1_radiobutton,'value');
  259. v2=get(handles.yuchuli2_radiobutton,'value');
  260. y=handles.y;fs=handles.Fs;
  261. if v1==1
  262. y=y-mean(y);
  263. else
  264. end
  265. if v2==1
  266.     y=detrend(y);
  267. else
  268. end
  269. if strcmp(item,'低通滤波')
  270.     fs=handles.Fs;
  271.     limit=handles.limit-500;
  272.     wp=pi*limit/fs;              %归一化通带数字频率wp
  273.     ws=pi*(limit+200)/fs;  %归一化阻带数字截止频率ws
  274.      Rp=0.5;                      %通带波纹系数Rp
  275.      Rs=60;  
  276.     [N,Wn]=buttord(wp,ws,Rp,Rs); %求阶数N和滤波器截止频率Wn
  277.     [num,den]=butter(N,Wn);         %传输分子和分母的系数
  278. else
  279.      fs=handles.Fs;
  280.      limit=handles.limit;
  281.      wp=pi*limit/fs;              %归一化通带数字频率wp
  282.      ws=pi*(limit-200)/fs;

  283.      Rp=0.3;                      %通带波纹系数Rp
  284.      Rs=80;  
  285.     [N,Wn]=buttord(wp,ws,Rp,Rs); %求阶数N和滤波器截止频率Wn
  286.     [num,den]=butter(N,Wn,'high');         %传输分子和分母的系数
  287. end
  288.     f=filter(num,den,y);                    %滤波
  289.     M=max(size(f));
  290.     s=length(f);
  291.     zh=fft(f,s);              %做FFT变换
  292.     v=0:fs/s:fs*(s-1)/s;  %计算频率
  293.     plot(handles.axes3,v(1:M/2),zh(1:M/2))
  294.     set(handles.axes3,'Xgrid','on');
  295.     set(handles.axes3,'Ygrid','on');
  296.     xlabel(handles.axes3,'频率');
  297.     ylabel(handles.axes3,'振幅');
  298.     title(handles.axes3,'滤波后的频谱图');
  299.    
  300.     plot(handles.axes1,v,handles.y);
  301.     title(handles.axes1,'原始时域图');
  302.     xlabel(handles.axes1,'时间');
  303.     ylabel(handles.axes1,'频率');
  304.     set(handles.axes1,'Xgrid','on');
  305.     set(handles.axes1,'Ygrid','on');
  306.    
  307.     N=length(handles.y);
  308.     y1=fft(handles.y,N);
  309.     f1=0:fs/N:fs*(N-1)/N;
  310.     plot(handles.axes2,f1(1:N/2),y1(1:N/2));
  311.     set(handles.axes2,'Xgrid','on');
  312.     set(handles.axes2,'Ygrid','on');
  313.     title(handles.axes2,'频谱图');
  314.     xlabel( handles.axes2,'频率');
  315.     ylabel( handles.axes2,'幅度');
  316.     handles.y=f;
  317.     handles.Fs=fs;
  318.     guidata(hObject,handles);



  319. % --- Executes on button press in close_pushbutton.
  320. function close_pushbutton_Callback(~, ~, ~)
  321. % hObject    handle to close_pushbutton (see GCBO)
  322. % eventdata  reserved - to be defined in a future version of MATLAB
  323. % handles    structure with handles and user data (see GUIDATA)
  324. close();

  325. % --- Executes on button press in fft_pushbutton.
  326. function fft_pushbutton_Callback(hObject, eventdata, handles)
  327. % hObject    handle to fft_pushbutton (see GCBO)
  328. % eventdata  reserved - to be defined in a future version of MATLAB
  329. % handles    structure with handles and user data (see GUIDATA)
  330. data=handles.y;
  331. Fs=handles.Fs;
  332. len=max(size(data));%读出声音数据文件的长度
  333. [sel,ok]=listdlg('ListString',{'默认长度','128','256','512','1024','2048','4096','8192','16384','32768','65536'},...
  334. 'name','选择FFT变换的点数', 'OKstring','确定', ...
  335. 'Cancelstring','取消', 'SelectionMode','single','ListSize',[300 200]);
  336. data_frequency=[len,128,256,512,1024,2048,4096,8192,16384,32768,65536];
  337. if ok==1
  338.     M=data_frequency(sel);
  339.     y=data(1:M);%取前M个数据做变换
  340.      N=length(y);
  341.      zh=fft(y,N);              %做FFT变换
  342.       f=0:Fs/N:Fs*(N-1)/N;  %计算频率
  343.      
  344.   plot(handles.axes2,f(1:M/2),zh(1:M/2))
  345.   title(handles.axes2,'傅里叶变换图像');
  346.   set(handles.axes2,'Xgrid','on');
  347.   set(handles.axes2,'Ygrid','on');
  348.   xlabel(handles.axes2,'频率');
  349.   ylabel(handles.axes2,'振幅');
  350.      
  351. else
  352. end
  353. handles.zh=zh;
  354. guidata(hObject,handles);


  355. % --- Executes on button press in fan_pushbutton.
  356. function fan_pushbutton_Callback(hObject, eventdata, handles)
  357. % hObject    handle to fan_pushbutton (see GCBO)
  358. % eventdata  reserved - to be defined in a future version of MATLAB
  359. % handles    structure with handles and user data (see GUIDATA)
  360. zh=handles.zh;
  361. y=ifft(zh);
  362. plot(handles.axes3,y);
  363. title(handles.axes3,'反傅里叶变换图像')
  364. xlabel(handles.axes3,'数据序列');
  365. ylabel(handles.axes3,'频率');
  366. set(handles.axes3,'Xgrid','on');
  367. set(handles.axes3,'Ygrid','on');
  368. handles.y=y;
  369. guidata(hObject,handles);


  370. % --- Executes on button press in b2g_pushbutton.
  371. function b2g_pushbutton_Callback(hObject, eventdata, handles)
  372. % hObject    handle to b2g_pushbutton (see GCBO)
  373. % eventdata  reserved - to be defined in a future version of MATLAB
  374. % handles    structure with handles and user data (see GUIDATA)
  375.       %读取音频信息(双声道,16位,频率44100Hz)
  376.       % 定义常数
  377.     FL = 80;                % 帧长
  378.     WL = 240;               % 窗长
  379.     P = 10;                 % 预测系数个数
  380.     data=handles.y;fs=handles.Fs;     % 载入语音数据
  381.         data= data/max(data);        %归一化
  382.     L = length(data);          % 读入语音长度
  383.     FN = floor(L/FL)-2;     % 计算帧数
  384.    
  385.         % 预测和重建滤波器
  386.     exc = zeros(L,1);       % 激励信号(预测误差)
  387.     zi_pre = zeros(P,1);    % 预测滤波器的状态
  388.     s_rec = zeros(L,1);     % 重建语音
  389.     zi_rec = zeros(P,1);
  390.    
  391.         % 合成滤波器
  392.     exc_syn = zeros(L,1);   % 合成的激励信号(脉冲串)
  393.     s_syn = zeros(L,1);     % 合成语音
  394.         last_syn = 0;   %存储上一个(或多个)段的最后一个脉冲的下标
  395.         zi_syn = zeros(P,1);   % 合成滤波器的状态
  396.    
  397.         % 变调不变速滤波器
  398.     exc_syn_t = zeros(L,1);   % 合成的激励信号(脉冲串)
  399.     s_syn_t = zeros(L,1);     % 合成语音
  400.         last_syn_t = 0;   %存储上一个(或多个)段的最后一个脉冲的下标
  401.         zi_syn_t = zeros(P,1);   % 合成滤波器的状态
  402.    
  403.         % 变速不变调滤波器(假设速度减慢一倍)
  404.        
  405.     hw = hamming(WL);       % 汉明窗
  406.    
  407.     % 依次处理每帧语音
  408.     for n = 3:FN

  409.         % 计算预测系数(不需要掌握)
  410.         s_w = data(n*FL-WL+1:n*FL).*hw;    %汉明窗加权后的语音
  411.         [A E] = lpc(s_w, P);            %用线性预测法计算P个预测系数
  412.                                         % A是预测系数,E会被用来计算合成激励的能量

  413.       
  414.         
  415.         s_f = data((n-1)*FL+1:n*FL);       % 本帧语音,下面就要对它做处理

  416.         % (4) 用filter函数s_f计算激励,注意保持滤波器状态
  417.                 [exc1,zi_pre] = filter(A,1,s_f,zi_pre);
  418.         
  419.         exc((n-1)*FL+1:n*FL) = exc1; %计算得到的激励

  420.         % (5) 用filter函数和exc重建语音,注意保持滤波器状态
  421.                 [s_rec1,zi_rec] = filter(1,A,exc1,zi_rec);
  422.         
  423.         s_rec((n-1)*FL+1:n*FL) = s_rec1; %计算得到的重建语音

  424.         % 注意下面只有在得到exc后才会计算正确
  425.         s_Pitch = exc(n*FL-222:n*FL);
  426.         PT = findpitch(s_Pitch);    % 计算基音周期PT(不要求掌握)
  427.         G = sqrt(E*PT);           % 计算合成激励的能量G(不要求掌握)

  428.                        
  429.                 %方法3:本段激励只能修改本段长度
  430.                 tempn_syn = [1:n*FL-last_syn]';
  431.                 exc_syn1 = zeros(length(tempn_syn),1);
  432.                 exc_syn1(mod(tempn_syn,PT)==0) = G; %某一段算出的脉冲
  433.                 exc_syn1 = exc_syn1((n-1)*FL-last_syn+1:n*FL-last_syn);
  434.                 [s_syn1,zi_syn] = filter(1,A,exc_syn1,zi_syn);
  435.                 exc_syn((n-1)*FL+1:n*FL) =  exc_syn1;   %计算得到的合成激励
  436.                 s_syn((n-1)*FL+1:n*FL) = s_syn1;   %计算得到的合成语音
  437.                 last_syn = last_syn+PT*floor((n*FL-last_syn)/PT);
  438.                
  439.       
  440.         
  441.         % (13) 将基音周期减小一半,将共振峰频率增加150Hz,重新合成语音,听听是啥感受~
  442.                 PT1 =floor(PT/2);   %减小基音周期
  443.         poles = roots(A);
  444.                 deltaOMG =150*2*pi/fs;
  445.                 for p=1:10   %增加共振峰频率,实轴上方的极点逆时针转,下方顺时针转
  446.                         if imag(poles(p))>0 poles(p) = poles(p)*exp(j*deltaOMG);
  447.                         elseif imag(poles(p))<0 poles(p) = poles(p)*exp(-j*deltaOMG);
  448.                         end
  449.                 end
  450.                 A1=poly(poles);
  451.        
  452.                
  453.                 tempn_syn_t = [1:n*FL-last_syn_t]';
  454.                 exc_syn1_t = zeros(length(tempn_syn_t),1);
  455.                 exc_syn1_t(mod(tempn_syn_t,PT1)==0) = G; %某一段算出的脉冲
  456.                 exc_syn1_t = exc_syn1_t((n-1)*FL-last_syn_t+1:n*FL-last_syn_t);
  457.                 [s_syn1_t,zi_syn_t] = filter(1,A1,exc_syn1_t,zi_syn_t);
  458.                 exc_syn_t((n-1)*FL+1:n*FL) =  exc_syn1_t;   %计算得到的合成激励
  459.                 s_syn_t((n-1)*FL+1:n*FL) = s_syn1_t;   %计算得到的合成语音
  460.                 last_syn_t = last_syn_t+PT1*floor((n*FL-last_syn_t)/PT1);
  461.         
  462.     end

  463.      plot(handles.axes3,s_syn_t),
  464.      set(handles.axes3,'Xgrid','on');
  465.      set(handles.axes3,'Ygrid','on');
  466.      xlabel(handles.axes3,'数据序列');
  467.      ylabel(handles.axes3,'频率');
  468.      title(handles.axes3,'变音后的时域图'),XLim([0,length(s_syn_t)]);       
  469.      handles.y=s_syn_t;
  470.      guidata(hObject,handles);
  471.      Fs=handles.foriginal;  
  472.      N=length(handles.y);
  473.     y1=fft(handles.y,N);
  474.     f1=0:Fs/N:Fs*(N-1)/N;
  475.     plot(handles.axes1,f1,handles.yoriginal);
  476.     title(handles.axes1,'原始时域图');
  477.     xlabel(handles.axes1,'时间');
  478.     ylabel(handles.axes1,'频率');
  479.     set(handles.axes1,'Xgrid','on');
  480.     set(handles.axes1,'Ygrid','on');
  481.    
  482.    
  483.     plot(handles.axes2,f1(1:N/2),y1(1:N/2));
  484.     set(handles.axes2,'Xgrid','on');
  485.     set(handles.axes2,'Ygrid','on');
  486.     title(handles.axes2,'频谱图');
  487.     xlabel( handles.axes2,'频率');
  488.     ylabel( handles.axes2,'幅度');
  489.    pause(3);
  490.         sound(handles.yoriginal);

  491.    sound(handles.y);

  492.   
  493.   
  494.    

  495. % --- Executes on button press in g2b_pushbutton.
  496. function g2b_pushbutton_Callback(hObject, eventdata, handles)
  497. % hObject    handle to g2b_pushbutton (see GCBO)
  498. % eventdata  reserved - to be defined in a future version of MATLAB
  499. % handles    structure with handles and user data (see GUIDATA)
  500. y=handles.y;
  501. fs=handles.Fs;%读取音频信息(双声道,16位,频率44100Hz)
  502. N=length(y)
  503. f=0:fs/N:fs*(N-1)/N;
  504. Y=fft(y,N);                %进行傅立叶变换
  505. plot(handles.axes2,f(1:N/2),Y(1:N/2));
  506. title(handles.axes2,'声音信号的频谱');
  507. xlabel(handles.axes2,'频率');
  508. ylabel(handles.axes2,'振幅');
  509. f1=0:(fs*0.7)/N:(fs*0.7)*(N-1)/N;
  510. syms t;
  511. t=[0,9];
  512. R=y*exp(2*pi*300*t);
  513. P=fft(R,N);
  514. Z=ifft(P);
  515. z=real(Z);
  516. handles.y=y;
  517. plot(handles.axes3,f1(1:N/2),Z(1:N/2));
  518. title(handles.axes3,'变声后的时域图');
  519. xlabel(handles.axes3,'时间序列');
  520. ylabel(handles.axes3,'频率')
  521. set(handles.axes3,'Xgrid','on');
  522. set(handles.axes3,'Ygrid','on');

  523. Fs=handles.foriginal;  
  524.      N=length(handles.y);
  525.     y1=fft(handles.y,N);
  526.     f1=0:Fs/N:Fs*(N-1)/N;
  527.     plot(handles.axes1,f1,handles.yoriginal);
  528.     title(handles.axes1,'原始时域图');
  529.     xlabel(handles.axes1,'时间');
  530.     ylabel(handles.axes1,'频率');
  531.     set(handles.axes1,'Xgrid','on');
  532.     set(handles.axes1,'Ygrid','on');
  533.    
  534.    
  535.     plot(handles.axes2,f1(1:N/2),y1(1:N/2));
  536.     set(handles.axes2,'Xgrid','on');
  537.     set(handles.axes2,'Ygrid','on');
  538.     title(handles.axes2,'频谱图');
  539.     xlabel( handles.axes2,'频率');
  540.     ylabel( handles.axes2,'幅度');
  541.    pause(3);
  542. guidata(hObject,handles);

  543. sound(handles.yoriginal);

  544. sound(handles.y,handles.foriginal*0.7);

  545. % --- Executes on button press in bijiao_pushbutton.
  546. function bijiao_pushbutton_Callback(hObject, eventdata, handles)
  547. % hObject    handle to bijiao_pushbutton (see GCBO)
  548. % eventdata  reserved - to be defined in a future version of MATLAB
  549. % handles    structure with handles and user data (see GUIDATA)
  550. handles.y=handles.yoriginal;
  551. handles.Fs=handles.foriginal;
  552. guidata(hObject,handles);

  553. % --- Executes on button press in save_pushbutton.
  554. function save_pushbutton_Callback(hObject, eventdata, handles)
  555. % hObject    handle to save_pushbutton (see GCBO)
  556. % eventdata  reserved - to be defined in a future version of MATLAB
  557. % handles    structure with handles and user data (see GUIDATA)
  558. Fs=handles.Fs;
  559. data=handles.y;
  560. [filename]=uiputfile({'*.wav'},'文件保存');
  561. wavwrite(data,Fs,filename);%data表示声音数据


  562. % 计算一段语音的基音周期
  563. function PT = findpitch(s)
  564. [B, A] = butter(5, 700/4000);
  565. s = filter(B,A,s);
  566. R = zeros(143,1);
  567. for k=1:143
  568.     R(k) = s(144:223)'*s(144-k:223-k);
  569. end
  570. [R1,T1] = max(R(80:143));
  571. T1 = T1 + 79;
  572. R1 = R1/(norm(s(144-T1:223-T1))+1);
  573. [R2,T2] = max(R(40:79));
  574. T2 = T2 + 39;
  575. R2 = R2/(norm(s(144-T2:223-T2))+1);
  576. [R3,T3] = max(R(20:39));
  577. T3 = T3 + 19;
  578. R3 = R3/(norm(s(144-T3:223-T3))+1);
  579. Top = T1;
  580. Rop = R1;
  581. if R2 >= 0.85*Rop
  582.     Rop = R2;
  583.     Top = T2;
  584. end
  585. if R3 > 0.85*Rop
  586.     Rop = R3;
  587.     Top = T3;
  588. end
  589. PT = Top;
  590. return
复制代码


发表于 2012-12-24 14:27 | 显示全部楼层
还有一段男女声识别程序

  1. %filename:manwoman.m
  2. %different man from woman.
  3. %===========================================================
  4. clear;
  5. if nargin<1;action='initialized';end;
  6. [fname,pname]=uigetfile('*.wav','Open Wave File');
  7. file=[pname,fname];
  8. [x,fs,bits]=wavread(file);       % 读入声音文件(*.wav)      
  9. sound(x,fs,bits);                % 数据通过声卡转换为声音
  10. %===========================================================
  11. % pause;
  12. data1=x(:,1);
  13. n=0:length(x)-1;
  14. time=n/fs;
  15. subplot(3,1,1)                                        % 绘制2行1列的第1张子图
  16. plot(time,data1)                                      % 以时间为横轴,数据为纵轴作图
  17. xlabel('Time (sec.)')                                 % 标注横坐标
  18. ylabel('Signal Level (Volts)')                        % 标注纵坐标
  19. grid on                                               % 添加网格
  20. % pause;
  21. % 对采集数据作滤波处理
  22. blocksize =length(data1);                             % 计算窗函数长度  
  23. window = hanning(blocksize);                   % 计算汉宁窗函数(此函数为MATLAB自带)
  24. data2=window.*data1;                                  % 对数据先作加窗处理
  25. % wp=[70,400];                                             %100Hz--400Hz
  26. % wp=wp*2/fs;
  27. wp=3000*2/fs;
  28. [b,a]=ellip(4,0.1,20,wp);                      % 构造椭圆滤波器
  29. data=filter(b,a,data2);                               % 求加窗处理后的数据经过滤波器的响应
  30. subplot(3,1,2)                                        % 绘制2行1列的第1张子图
  31. plot(time,data)                                      % 以时间为横轴,数据为纵轴作图
  32. xlabel('Time (sec.)')                                 % 标注横坐标
  33. %ylabel('Signal Level (Volts)')                        % 标注纵坐标
  34. grid on                                               % 添加网格
  35. % pause;
  36. [xmax,index]=max(data1);
  37. timewin=floor(0.015*fs);
  38. xwin=data1(index-timewin:index+timewin);
  39. [y,lags]=xcov(xwin);
  40. subplot(3,1,3)
  41. plot(lags,y)
  42. grid on
  43. ylen=length(y);
  44. halflen=(ylen+1)/2 +30;
  45. yy=y(halflen: ylen);
  46. [ymax,maxindex] = max(yy);
  47. fmax=fs/(maxindex+30);
  48. disp(['Maximum occurred at ', num2str(fmax), ' Hz'])
复制代码


 楼主| 发表于 2012-12-25 22:29 | 显示全部楼层
发表于 2012-12-29 09:54 | 显示全部楼层
戚方 发表于 2012-12-25 22:29
先谢谢您,还有点想问地就是第二个程序前面的我都会,变声部分能给我注释一下吗?[em:16:]

第二个是男女生识别,不是变声
主要是求相关性等,你看一下相关命令的帮助文件吧
 楼主| 发表于 2012-12-29 10:05 | 显示全部楼层
happy 发表于 2012-12-29 09:54
第二个是男女生识别,不是变声
主要是求相关性等,你看一下相关命令的帮助文件吧

恩,我会相关性分析。通过自相关判断信号基频等区分男女声音。第一个变声是通过什么实现的?频谱搬迁吗?我不会频谱移动,给我段频谱搬迁的程序吧。
发表于 2012-12-29 10:13 | 显示全部楼层
戚方 发表于 2012-12-29 10:05
恩,我会相关性分析。通过自相关判断信号基频等区分男女声音。第一个变声是通过什么实现的?频谱搬迁吗? ...

参考第一个程序的b2g_pushbutton_Callback部分
您需要登录后才可以回帖 登录 | 我要加入

本版积分规则

QQ|小黑屋|Archiver|手机版|联系我们|声振论坛

GMT+8, 2024-11-5 22:49 , Processed in 0.066327 second(s), 17 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表