我如何对 .wav 文件进行下采样,然后使用奈奎斯特重建它? - 在 MATLAB 中
这一切都是在 MATLAB 2010 中完成的。
我的目标是显示以下结果:欠采样、奈奎斯特率/过采样
首先,我需要对 .wav 文件进行下采样以获得不完整/或公正的数据流,然后我可以重新构建该数据流。
这是我要做的事情的流程图所以流程是模拟信号 - >采样模拟滤波器-> ADC->向下重新采样 ->重新采样 -> DAC->重构模拟滤波器
需要实现什么:
F=频率
F(Hz=1/s) Ex 100Hz = 1000(周期/秒) F(s)= 1/(2f)
示例问题:1000 Hz = 最高 频率 1/2(1000hz) = 1/2000 = 5x10(-3) 秒/周期或采样率 5毫秒
这是我使用 matlab 的第一个信号处理项目。
到目前为止我所拥有的。
% Fs = frequency sampled (44100hz or the sampling frequency of a cd)
[test,fs]=wavread('test.wav'); % loads the .wav file
left=test(:,1);
% Plot of the .wav signal time vs. strength
time=(1/44100)*length(left);
t=linspace(0,time,length(left));
plot(t,left)
xlabel('time (sec)');
ylabel('relative signal strength')
**%this is were i would need to sample it at the different frequecys (both above and below and at) nyquist frequency.*I think.***
soundsc(left,fs) % shows the resaultant audio file , which is the same as original ( only at or above nyquist frequency however)
谁能告诉我如何让它变得更好,以及如何以不同的频率进行采样?
这是 .wav 文件 http://www.4shared.com/audio/11xvNmkd/piano。 html
编辑:
%Play decimated file ( soundsc(y,fs) )
%Play Original file ( soundsc(play,fs ) )
%Play reconstucted File ( soundsc(final,fs) )
[piano,fs]=wavread('piano.wav'); % loads piano
play=piano(:,1); % Renames the file as "play"
t = linspace(0,time,length(play)); % Time vector
x = play;
y = decimate(x,25);
stem(x(1:30)), axis([0 30 -2 2]) % Original signal
title('Original Signal')
figure
stem(y(1:30)) % Decimated signal
title('Decimated Signal')
%changes the sampling rate
fs1 = fs/2;
fs2 = fs/3;
fs3 = fs/4;
fs4 = fs*2;
fs5 = fs*3;
fs6 = fs*4;
wavwrite(y,fs/25,'PianoDecimation');
%------------------------------------------------------------------
%Downsampled version of piano is now upsampled to the original
[PianoDecimation,fs]=wavread('PianoDecimation.wav'); % loads piano
play2=PianoDecimation(:,1); % Renames the file as "play
%upsampling
UpSampleRatio = 2; % 2*fs = nyquist rate sampling
play2Up=zeros(length(PianoDecimation)*UpSampleRatio, 1);
play2Up(1:UpSampleRatio:end) = play2; % fill in every N'th sample
%low pass filter
ResampFilt = firpm(44, [0 0.39625 0.60938 1], [1 1 0 0]);
fsUp = (fs*UpSampleRatio)*1;
wavwrite(play2Up,fsUp,'PianoUpsampled');
%Plot2
%data vs time plot
time=(1/44100)*length(play2);
t=linspace(0,time,length(play2));
stem(t,play2)
title('Upsampled graph of piano')
xlabel('time(sec)');
ylabel('relative signal strength')
[PianoUpsampled,fs]=wavread('PianoUpsampled.wav'); % loads piano
final=PianoUpsampled(:,1); % Renames the file as "play"
%-------------------------------------------------------------
%resampleing
[piano,fs]=wavread('piano.wav'); % loads piano
x=piano(:,1); % Renames the file as "play"
m = resample(x,3,2);
This is all done in MATLAB 2010
My objective is to show the results of: undersampling, nyquist rate/ oversampling
First i need to downsample the .wav file to get an incomplete/ or impartial data stream that i can then reconstuct.
Heres the flow chart of what im going to be doing So the flow is analog signal -> sampling analog filter -> ADC -> resample down -> resample up -> DAC -> reconstruction analog filter
what needs to be achieved:
F= Frequency
F(Hz=1/s) E.x. 100Hz = 1000 (Cyc/sec)
F(s)= 1/(2f)Example problem: 1000 hz = Highest
frequency 1/2(1000hz) = 1/2000 =
5x10(-3) sec/cyc or a sampling rate of
5ms
This is my first signal processing project using matlab.
what i have so far.
% Fs = frequency sampled (44100hz or the sampling frequency of a cd)
[test,fs]=wavread('test.wav'); % loads the .wav file
left=test(:,1);
% Plot of the .wav signal time vs. strength
time=(1/44100)*length(left);
t=linspace(0,time,length(left));
plot(t,left)
xlabel('time (sec)');
ylabel('relative signal strength')
**%this is were i would need to sample it at the different frequecys (both above and below and at) nyquist frequency.*I think.***
soundsc(left,fs) % shows the resaultant audio file , which is the same as original ( only at or above nyquist frequency however)
Can anyone tell me how to make it better, and how to do the sampling at verious frequencies?
heres the .wav file http://www.4shared.com/audio/11xvNmkd/piano.html
EDIT:
%Play decimated file ( soundsc(y,fs) )
%Play Original file ( soundsc(play,fs ) )
%Play reconstucted File ( soundsc(final,fs) )
[piano,fs]=wavread('piano.wav'); % loads piano
play=piano(:,1); % Renames the file as "play"
t = linspace(0,time,length(play)); % Time vector
x = play;
y = decimate(x,25);
stem(x(1:30)), axis([0 30 -2 2]) % Original signal
title('Original Signal')
figure
stem(y(1:30)) % Decimated signal
title('Decimated Signal')
%changes the sampling rate
fs1 = fs/2;
fs2 = fs/3;
fs3 = fs/4;
fs4 = fs*2;
fs5 = fs*3;
fs6 = fs*4;
wavwrite(y,fs/25,'PianoDecimation');
%------------------------------------------------------------------
%Downsampled version of piano is now upsampled to the original
[PianoDecimation,fs]=wavread('PianoDecimation.wav'); % loads piano
play2=PianoDecimation(:,1); % Renames the file as "play
%upsampling
UpSampleRatio = 2; % 2*fs = nyquist rate sampling
play2Up=zeros(length(PianoDecimation)*UpSampleRatio, 1);
play2Up(1:UpSampleRatio:end) = play2; % fill in every N'th sample
%low pass filter
ResampFilt = firpm(44, [0 0.39625 0.60938 1], [1 1 0 0]);
fsUp = (fs*UpSampleRatio)*1;
wavwrite(play2Up,fsUp,'PianoUpsampled');
%Plot2
%data vs time plot
time=(1/44100)*length(play2);
t=linspace(0,time,length(play2));
stem(t,play2)
title('Upsampled graph of piano')
xlabel('time(sec)');
ylabel('relative signal strength')
[PianoUpsampled,fs]=wavread('PianoUpsampled.wav'); % loads piano
final=PianoUpsampled(:,1); % Renames the file as "play"
%-------------------------------------------------------------
%resampleing
[piano,fs]=wavread('piano.wav'); % loads piano
x=piano(:,1); % Renames the file as "play"
m = resample(x,3,2);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
最简单的方法是将采样率更改为整数倍。 下采样包括通过低通滤波器运行数据,然后丢弃样本,而上采样包括插入样本,然后通过低通滤波器运行数据(也称为重建滤波器或插值滤波器)。当过滤步骤被跳过或做得不好时,就会出现混叠。因此,为了显示锯齿的效果,我建议您只需根据需要丢弃或插入样本,然后以新的采样率创建一个新的 WAV 文件。要丢弃样本,您可以执行以下操作:
要创建样本,您可以执行以下操作:
您只需播放写入的 WAV 文件即可听到效果。
顺便说一句,您要求改进代码 - 我更喜欢将 t 向量初始化为 t = (0:(length(left)-1))/fs; 。
The easiest thing to do is change sample rates by an integer factor. Downsampling consists of running the data through a low-pass filter followed by discarding samples, while upsampling consists of inserting samples then running the data through a low pass filter (also known as a reconstruction filter or interpolating filter). Aliasing occurs when the filtering steps are skipped or poorly done. So, to show the effect of aliasing, I suggest you simply discard or insert samples as required, then create a new WAV file at the new sample rate. To discard samples, you can do:
To create samples you can do:
You can just play back the written WAV files to hear the effects.
As an aside, you asked for improvements to your code - I prefer to initialize the
t
vector ast = (0:(length(left)-1))/fs;
.您需要的 DSP 技术称为抽取。
The DSP technique you need is called decimation.