1.读取wav音乐文件
% 读取音频文件 filename = 'jay.wav'; [sound_data, fs] = audioread(filename); % 9507502x2 44100 sound_data_1 = sound_data(:, 1); sound_data_1 = sound_data_1'; % 转置
sound_data有两列,因为此音乐文件有两个通道,音频采样率为44100;
这里只使用其中一个通道数据;
将多行数据转置为单行数据,方便后续调制
2.调制
% 调制参数 carrier_freq = 1000; % 载波频率 modulation_index = 0.5; % 调制指数 dc = 1; % 直流偏置 % 创建载波信号 t = (0:length(sound_data_1)-1) / fs; carrier = cos(2*pi*carrier_freq*t); % 进行AM调制 sound_data_1 = dc + modulation_index*sound_data_1; %直流偏置,乘以调制指数在偏置 modulated_signal = sound_data_1 .* carrier;
关于一些参数:
调制指数:也叫调制深度,定义为调制信号振幅与载波振幅之间的比例。调制指数决定了调制信号对载波振幅的影响程度。
直流偏置:AM调制时需要将基带信号往上搬移的幅值,保证信号所有采样点都在正轴
3.包络检波解调
%%%%%%%%%%%%%%1.使用接口解调,包络检波%%%%%%%%%%%%% % % 解调恢复原始信号 % demodulated_signal = amdemod(modulated_signal, carrier_freq, fs, 0, modulation_index); % % 播放解调后的音频 %sound(demodulated_signal, fs); %%%%%%%%%%%%%%2.希尔伯特变换的包络检波%%%%%%%%%%%%% % demodulated_signal = abs(hilbert(modulated_signal)); % sound(demodulated_signal, fs); %%%%%%%%%%%%%%3.接口的包络检波,等于希尔伯特%%%%%%%%%%%%% % [upper_signal, lower_signal] = envelope(modulated_signal); % sound(upper_signal, fs); % fs为采样率 % subplot(2, 1, 1); % plot(sound_data_1); % subplot(2, 1, 2); % plot(upper_signal);
4.相干解调
%%%%%%%%%%%%%%4.相干解调%%%%%%%%%%%%% demodulated_signal = modulated_signal .* carrier; % 设计低通滤波器 [b, a] = butter(6, carrier_freq/(fs/2)); % 设计6阶低通滤波器 % 低通滤波 filtered = filter(b, a, demodulated_signal); subplot(2, 1, 1); plot(sound_data_1); subplot(2, 1, 2); plot(filtered); sound(filtered, fs); % fs为采样率
5.FFT分析
%%%%%%%%%%%%%%%%%%%绘图%%%%%%%%%%%%%%%%%% % fft点数 fft_points = length(sound_data_1); %%%%%%%%%%%%%%画原始信号的fft%%%%%%%%%%%%% subplot(4, 1, 1); x1 = fs*(0:(fft_points-1))/fft_points; y1 = abs(fft(sound_data(:, 1), fft_points)); plot(x1, y1); xlabel('Frequency (Hz)'); ylabel('Magnitude'); title('FFT Spectrum'); %%%%%%%%%%%%%%画加了偏置原始信号的fft%%%%%%%%%%%%% subplot(4, 1, 2); x2 = fs*(0:(fft_points-1))/fft_points; y2 = abs(fft(sound_data_1, fft_points)); plot(x2, y2); xlabel('Frequency (Hz)'); ylabel('Magnitude'); title('FFT Spectrum'); %%%%%%%%%%%%%画am调制信号的fft%%%%%%%%%%%%% subplot(4, 1, 3); x3 = fs*(0:(fft_points-1))/fft_points; y3 = abs(fft(modulated_signal, fft_points)); plot(x3, y3); xlabel('Frequency (Hz)'); ylabel('Magnitude'); title('FFT Spectrum'); %%%%%%%%%%%%%画解调调制信号的fft%%%%%%%%%%%%% subplot(4, 1, 4); x4 = fs*(0:(fft_points-1))/fft_points; y4 = abs(fft(demodulated_signal, fft_points)); plot(x4, y4); xlabel('Frequency (Hz)'); ylabel('Magnitude'); title('FFT Spectrum');
6.完整代码
% AM调制 % 读取音频文件 filename = 'jay.wav'; [sound_data, fs] = audioread(filename); % 9507502x2 44100 sound_data_1 = sound_data(:, 1); sound_data_1 = sound_data_1'; % 转置 % 调制参数 carrier_freq = 1000; % 载波频率 modulation_index = 1; % 调制指数 dc = 1; % 直流偏置 % 创建载波信号 t = (0:length(sound_data_1)-1) / fs; carrier = cos(2*pi*carrier_freq*t); % 进行AM调制 sound_data_1 = dc + modulation_index*sound_data_1; %直流偏置,乘以调制指数在偏置 modulated_signal = sound_data_1 .* carrier; %%%%%%%%%%%%%%1.使用接口解调,包络检波%%%%%%%%%%%%% % % 解调恢复原始信号 % demodulated_signal = amdemod(modulated_signal, carrier_freq, fs, 0, modulation_index); % % 播放解调后的音频 %sound(demodulated_signal, fs); %%%%%%%%%%%%%%2.希尔伯特变换的包络检波%%%%%%%%%%%%% % demodulated_signal = abs(hilbert(modulated_signal)); % sound(demodulated_signal, fs); %%%%%%%%%%%%%%3.接口的包络检波,等于希尔伯特%%%%%%%%%%%%% % [upper_signal, lower_signal] = envelope(modulated_signal); % sound(upper_signal, fs); % fs为采样率 % subplot(2, 1, 1); % plot(sound_data_1); % subplot(2, 1, 2); % plot(upper_signal); %%%%%%%%%%%%%%4.相干解调%%%%%%%%%%%%% demodulated_signal = modulated_signal .* carrier; % 设计低通滤波器 [b, a] = butter(6, carrier_freq/(fs/2)); % 设计6阶低通滤波器 % 低通滤波 filtered = filter(b, a, demodulated_signal); subplot(2, 1, 1); plot(sound_data_1); subplot(2, 1, 2); plot(filtered); sound(filtered, fs); % fs为采样率 % %%%%%%%%%%%%%%%%%%%绘图%%%%%%%%%%%%%%%%%% % % fft点数 % fft_points = length(sound_data_1); % %%%%%%%%%%%%%%画原始信号的fft%%%%%%%%%%%%% % subplot(4, 1, 1); % x1 = fs*(0:(fft_points-1))/fft_points; % y1 = abs(fft(sound_data(:, 1), fft_points)); % plot(x1, y1); % xlabel('Frequency (Hz)'); % ylabel('Magnitude'); % title('FFT Spectrum'); % % %%%%%%%%%%%%%%画加了偏置原始信号的fft%%%%%%%%%%%%% % subplot(4, 1, 2); % x2 = fs*(0:(fft_points-1))/fft_points; % y2 = abs(fft(sound_data_1, fft_points)); % plot(x2, y2); % xlabel('Frequency (Hz)'); % ylabel('Magnitude'); % title('FFT Spectrum'); % % %%%%%%%%%%%%%画am调制信号的fft%%%%%%%%%%%%% % subplot(4, 1, 3); % x3 = fs*(0:(fft_points-1))/fft_points; % y3 = abs(fft(modulated_signal, fft_points)); % plot(x3, y3); % xlabel('Frequency (Hz)'); % ylabel('Magnitude'); % title('FFT Spectrum'); % % %%%%%%%%%%%%%画解调调制信号的fft%%%%%%%%%%%%% % subplot(4, 1, 4); % x4 = fs*(0:(fft_points-1))/fft_points; % y4 = abs(fft(demodulated_signal, fft_points)); % plot(x4, y4); % xlabel('Frequency (Hz)'); % ylabel('Magnitude'); % title('FFT Spectrum');
标签:sound,fs,signal,fft,AM,音频文件,points,Matlab,data From: https://www.cnblogs.com/judes/p/17521702.html