diff --git a/matlab/resampler/arb_filter.mat b/matlab/resampler/arb_filter.mat new file mode 100644 index 000000000..735fbc233 Binary files /dev/null and b/matlab/resampler/arb_filter.mat differ diff --git a/matlab/resampler/arb_resample_linear.m b/matlab/resampler/arb_resample_linear.m new file mode 100644 index 000000000..4bfb4df03 --- /dev/null +++ b/matlab/resampler/arb_resample_linear.m @@ -0,0 +1,46 @@ +function out = arb_resample_linear(sig, rate) + +N = 32; +M = 8; +step = (1/rate)*N; + +load('arb_filter.mat'); + +figure;plot(Num);title('Filter impulse response'); +[h,w] = freqz(Num, 1); +figure;plot(20*log10(abs(h)));title('Filter freq response'); + +% Create polyphase partition +poly = reshape(Num, N, M); + +% Filter +sig = [zeros(1,(M/2)-1) sig]; +k=0; +acc=0; +index=0; +frac=0; +out = []; +while k < length(sig)-M + sig_reg = fliplr(sig(k+1:k+M)); + filt_reg1 = poly(index+1, :); + filt_reg2 = poly(mod(index+1,N)+1, :); + res1 = sig_reg*filt_reg1'; + res2 = sig_reg*filt_reg2'; + + if index+1 == 32 + res = res1; + else + res = res1 + (res2-res1)*frac; + end + + out = [out res]; + + acc = acc+step; + index = fix(acc); + while index >= N + acc = acc - N; + index = index - N; + k = k+1; + end + frac = abs(acc-index); +end diff --git a/matlab/resampler/arb_resample_nearest.m b/matlab/resampler/arb_resample_nearest.m new file mode 100644 index 000000000..0601aa169 --- /dev/null +++ b/matlab/resampler/arb_resample_nearest.m @@ -0,0 +1,37 @@ +function out = arb_resample_nearest(sig, rate) + +N = 32; +M = 8; +step = (1/rate)*N; + +load('arb_filter.mat'); + +figure;plot(Num);title('Filter impulse response'); +[h,w] = freqz(Num, 1); +figure;plot(20*log10(abs(h)));title('Filter frequency response'); + +% Create polyphase partition +poly = reshape(Num, N, M); + +% Filter +sig = [zeros(1,(M/2)-1) sig]; +k=0; +acc=0; +index=0; +frac=0; +out = []; +while k < length(sig)-M + sig_reg = fliplr(sig(k+1:k+M)); + filt_reg1 = poly(index+1, :); + res = sig_reg*filt_reg1'; + + out = [out res]; + + acc = acc+step; + index = round(acc); + while index >= N + acc = acc - N; + index = index - N; + k = k+1; + end +end \ No newline at end of file diff --git a/matlab/resampler/arb_resample_test.m b/matlab/resampler/arb_resample_test.m new file mode 100644 index 000000000..b6d661da4 --- /dev/null +++ b/matlab/resampler/arb_resample_test.m @@ -0,0 +1,33 @@ +close all +clear all + +up = 24; +down = 25; +d_rate = up/down; +Fs = 100; % Arbitrary sample rate (used for displays) +Fsin = 1; + +% Create a sine wave +t = 0:1/Fs:1-1/Fs; +sig = sin(2*pi*t); + +out = arb_resample_nearest(sig,d_rate); + +% matlab resample for comparison +out2 = resample(sig, up, down); +figure;hold on;title('Ours and matlabs'); +l = min(length(out), length(out2)); +stem(out(1:l)); +stem(out2(1:l), 'r', 'filled'); +diff = out2(1:l)-out(1:l); +figure;plot(diff);title('Difference between ours and matlabs'); + +figure; hold on;title('Original and resampled - no time scaling'); +stem(sig); +stem(out, 'r', 'filled'); + +% Time align and plot +figure;hold on;title('Original and resampled - with time scaling'); +stem((1:75)/Fs,real(sig(1:75))); +stem((1:72)/(Fs*d_rate),real(out(1:72)),'r','filled'); +xlabel('Time (sec)');ylabel('Signal value'); \ No newline at end of file