Using interpolation and repetition to change the frequency of the signal in the octave and matlab

I am using Octave 3.8.1, which is similar to matlab, I am trying to use interpolation and repmat to change the frequency of the signals (since it does it so quickly (0.01 seconds), and I need to create 28000+ at a time) I can change the variable num_per_sec to any integer, but if I try to change it to 2.1 or something with a decimal digit, I get the error message : reshape: can't change the size of the array 44100x2 to an array error 92610x1: called from: line 10 (line repmat ) "Does anyone have work around this or another offer?

Note: please note that ya is a simple test equation . I will not have equations to work only with the signal to work, so just changing the variable frequency will not work.

see code below:

clear,clc
fs = 44100;                   % Sampling frequency
t=linspace(0,2*pi,fs);
freq=1;
ya = sin(freq*t)';  %please note that this is a simple test equation I won't have equations just the signal to work with.

num_per_sec=2 %works
%num_per_sec=2.1 %doesn't work
yb=repmat(ya,num_per_sec,1);%replicate matrix 
xxo=linspace(0,1,length(yb))'; %go from 0 to 1 sec can change speed by incr/decr 1
xxi=linspace(0,1,length(ya))'; %go from 0 to 1 sec and get total samplerate from total y value
yi_t=interp1(xxo,yb,xxi,'linear');

plot(yi_t)
0
source share
1 answer

You are trying to call repmatwith a floating point number. Obviously, this will not work the way you planned. repmatThe intended operation is replication of a certain size an integer number of times .

, , , 1 . , 2,4 , , 40% . 40% .

, , . , , , , , . . , 2.4, floor(0.4*fs), , , .

- :

%// Your code
clear, clc
fs = 44100; %// Define sampling frequency                 
t=linspace(0,2*pi,fs);
freq=1;
ya = sin(freq*t)'; %// Define signal

num_per_sec=2.1; %// Define total number of times we see the signal

%// New code
%// Get total number of integer times we see the signal
num_whole = floor(num_per_sec);

%// Replicate signal
yb=repmat(ya,num_whole,1);

%// Determine how many samples the partial signal consists of
portion = floor((num_per_sec - num_whole)*fs);

%// Sample from the original signal and stack this on top of replicated signal
yb = [yb; ya(1:portion)];

%// Your code
xxo=linspace(0,1,length(yb))'; 
xxi=linspace(0,1,length(ya))'; 
yi_t=interp1(xxo,yb,xxi,'linear');
+2

All Articles