将信号从 4 秒插值到 1 秒
我有一个从 0 到 4 秒循环两次的信号,我想插入信号以在从 0 到 1 秒时循环两次。我知道这是我的 xi 变量的问题;我只是不知道如何解决它。
该示例只是一个简单的正弦波方程,但我将在实际中导入音频 wav 文件;这就是我选择使用插值的原因。不幸的是,这不仅仅是一个简单的情节变化,因为它将是一个将被导入的音频文件,对其进行一些计算,然后作为另一个音频文件导出。
%Interpolation test
clear all, clc,clf,tic
x= linspace(0,2*pi,400); %from 0 to 4 sec
fs_rate=100
freq=2;
y=sin(freq*(x));
xo=linspace(0,length(y)/fs_rate,length(y)); %go from 0 to x sec
xi=linspace(0,1,length(y)); %go from 0 to 1 sec
new_y=interp1(xo,y,xi,'linear');
subplot(2,2,1),plot(xo,y),title('Orginal signal over 4 sec')
subplot(2,2,3),plot(xi,new_y),title('Entire signal over 1 sec')
我回去做了 Sergei 建议的操作,并使用了 resample 和 repmat,但我注意到在某些值上,行与采样率不同(见下图)。
请注意,顶部图像的 rows 值显示为 1000,底部图像显示 rows = 1008。当我仅更改某些值时,会发生这种情况。我该如何解决这个问题?我可以删除 1000 之后的所有内容,但我不确定这是一个错误还是 resample/repmat 的工作方式。
这是我用来测试的代码这:
%resample_repmat signal
clear all, clf
Fs = 1000; % Sampling rate
Ts = 1/Fs; %sampling interval
t=0:Ts:1-Ts; %sampling period
freq_orig=1;
y=sin(2*pi*t*freq_orig)'; %gives a short wave
freq_new=9;
y2=resample(y,1,freq_new); %resample matrix
y3=repmat (y2,freq_new,1); %replicate matrix
[r_orig,c_orig] = size(y) %get orig number of rows and cols
[r_new,c_new] = size(y3) %get new number of rows and cols
subplot(2,1,1),plot(y),title('Orginal signal')
title(['rows=',num2str(r_orig),' cols=',num2str(c_orig)])
subplot(2,1,2),plot(y3),title('New signal')
title(['rows=',num2str(r_new),' cols=',num2str(c_new)])
I have a signal that cycles twice from 0 to 4 seconds and I would like to interpolate the signal to cycle twice when it goes from 0 to 1 second. I know it's an issue with my xi
variable; I'm just not sure how to fix it.
The example is just a simple sine wave equation but I'll be importing an audio wav file in the real one; that's why I chose to use interpolate. Unfortunately it can't just be a simple plot change due to the fact it will be an audio file that will be imported, some calculations done on it, then exported back out as another audio file.
%Interpolation test
clear all, clc,clf,tic
x= linspace(0,2*pi,400); %from 0 to 4 sec
fs_rate=100
freq=2;
y=sin(freq*(x));
xo=linspace(0,length(y)/fs_rate,length(y)); %go from 0 to x sec
xi=linspace(0,1,length(y)); %go from 0 to 1 sec
new_y=interp1(xo,y,xi,'linear');
subplot(2,2,1),plot(xo,y),title('Orginal signal over 4 sec')
subplot(2,2,3),plot(xi,new_y),title('Entire signal over 1 sec')
I went back and did what Sergei recommended and used resample and repmat, but I'm noticing that on some of the values the rows aren't the same as the sample rate (see image below).
Notice the top image value for rows says 1000 and the bottom image says rows = 1008. This happens when I change the values of resample and repmat (freq_new) but only for certain values. How can I fix this? I could just delete everything after 1000 but I'm not sure if this is a bug or just the way resample/repmat works.
Here's the code I used to test this:
%resample_repmat signal
clear all, clf
Fs = 1000; % Sampling rate
Ts = 1/Fs; %sampling interval
t=0:Ts:1-Ts; %sampling period
freq_orig=1;
y=sin(2*pi*t*freq_orig)'; %gives a short wave
freq_new=9;
y2=resample(y,1,freq_new); %resample matrix
y3=repmat (y2,freq_new,1); %replicate matrix
[r_orig,c_orig] = size(y) %get orig number of rows and cols
[r_new,c_new] = size(y3) %get new number of rows and cols
subplot(2,1,1),plot(y),title('Orginal signal')
title(['rows=',num2str(r_orig),' cols=',num2str(c_orig)])
subplot(2,1,2),plot(y3),title('New signal')
title(['rows=',num2str(r_new),' cols=',num2str(c_new)])
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我可能不完全理解这个问题,但听起来您实际上对执行插值并不感兴趣,而只是对时间进行压缩?只需将原始
y
与新时间向量xi
结合使用,您就应该获得所需的结果:I might not fully understand the question, but it doesn't sound like you're actually interested in performing an interpolation, but rather just a compression in time? You should get your desired result by just using the original
y
with your new time vectorxi
:您需要使用某种FFT。以下链接解释了一些时间压缩方法(顺便说一句,第一个 Google 结果):http://dspdimension .com/admin/time-pitch-overview。
You'll want to use some kind of FFT. Here is a link that explains some methods for time compression (1st Google result btw): http://dspdimension.com/admin/time-pitch-overview.