I'm trying to create a seamless loop using a "non-periodic" signal using interpolation to smooth out the beginning and the end but I'm still getting a click at the beginning when it loops and I listen to it. Can interpolation do this?

Please Note that the equation is done this way to create a "non-periodic" signal to test when a signal does not start and end at the zero crossings.

I used audacity to zoom in and play the loops. I've included images along with the matlab/octave code.

Entire Signal

Begining of signal

End of signal

% combines sig to create seamless loop
clear all,clc,tic;
dirpathtmp=strcat('/tmp/'); %/home/rat/Documents/octave/eq_research/main/transform/voice

t1=linspace(0,2*pi,fs); %need to round off no decimals
t2=linspace(0,2*pi,fs); %need to round off no decimals
t3=linspace(0,2*pi,fs); %need to round off no decimals

%Create signal in different arrays

ytnorm=(yt/max(abs(yt))*.8); %normalize signal 

%change end points to stddev number

%for loop to replace points to create seamless loop
ytnorm(1)=0; %set first point to 0
ytnorm(fs)=0; %set last point to zero

for kk=2:1:15;

wavwrite([ytnorm'] ,fs,16,strcat(dirpathtmp,fn)); 


fprintf('\nfinally Done-elapsed time -%4.4fsec- or -%4.4fmins- or -%4.4fhours-\n',toc,toc/60,toc/3600);
  • $\begingroup$ I have updated my answer to show how you could make the start and end point match without an abrupt change. $\endgroup$ Jan 31 '13 at 14:46
  • 1
    $\begingroup$ Why not just window the signal so that the beginning and end are at zero? $\endgroup$
    – Peter K.
    Jan 31 '13 at 15:15

Not sure if I understand it correctly, but assuming this is your problem: I have a signal that i want to repeat, but the beginning is not at the same level as the end

The solution can be quite straightforward:

Calculate the trend (end-begin)/length of interval and correct the sound value at each point in the interval for this trend.

If you already tried this, please plot the entire signal played twice and look whether you still see something strange in the middle.

Here is an example of what you can do to correct for the trend instead of interpolation:

ydiff = ytnorm(end) - ytnorm(1);
ymean = mean(ytnorm);
ytnorm = ytnorm - (1:length(ytnorm)).*(ydiff /length(ytnorm)) ;
ytnorm = ytnorm - mean(ytnorm) + ymean;
  • $\begingroup$ this is what interpolation is suppose to do (see the interp1 command in the for loop) but as you can see it doesn't do a gradual spline $\endgroup$
    – Rick T
    Jan 31 '13 at 11:29
  • $\begingroup$ @RickT I am saying that you may not want to interpolate between points, but rather correct for the trend in the signal. $\endgroup$ Jan 31 '13 at 13:21
  • $\begingroup$ Thanks I know this is an old question but does the code you used for your answer have a technical name for it? Or is it just call trend correction? $\endgroup$
    – Rick T
    Sep 27 '14 at 11:15
  • $\begingroup$ @RickT I am not aware of a better description, but if you are looking for a single term you would probably be ok with 'detrending'.-- In general the removal of properties of a dataset could be called normalization so it's good to know the term, but if you say you will normalize the signal that could basically mean anything so I would not recommend it. $\endgroup$ Sep 29 '14 at 8:05

An alternate method is to create an array of two copies of your input signal

y = [ x , x ]         % MATLAB
y = hstack(( x , x )) # NUMPY

Run $x[k]$ through a LPF. Then slice $N$ samples from the "middle" of $y[k]$ where $N$ is the length of $x[k]$.

xsmooth = y( o : o + N - 1) % MATLAB
xsmooth = y[ o : o + N ]    # NUMPY

Where $o$ is an appropriately chosen offset. Be mindful of filter "tails" if you carry out the LPF through conventional convolution.


You're still getting clicks because you're forcing your signal to zero abprubtly. (Abrupt changes in time correspond to wide-band signals in frequency domain which account for the click-like sound). I think interpolation is not the suitable approach here as it involves the insertion of additional data points, what you probably don't want.

Fading the signal in/out can solve the problem. This can be a linear rise/decay or some spline curve. I'm quite sure Audacity can do this. Of course, you can also do it in Matlab by multiplying the beginning/end of your signal with some fading curve like this:

% Fade In
L = 1000; % length of fade in
y_start = 0; % start value
y_stop = 1; % stop value
lin_fading_curve = linspace(y_start, y_stop, L); % linear fading curve
cub_fading_curve = 1.5 * lin_fading_curve - 0.5 * lin_fading_curve .^ 3; % cubic fading curve
y_lin_fade_in = [ytnorm(1:L) .* lin_fading_curve, ytnorm(L+1:end)];
y_cub_fade_in = [ytnorm(1:L) .* cub_fading_curve, ytnorm(L+1:end)];

hold on;    
plot(t1, y_lin_fade_in, 'r');
plot(t1, y_cub_fade_in, 'g');
xlim([0 2*pi/fs*1200]);
legend('original', 'linear', 'cubic', 'Location', 'SouthEast');
hold off;

Here's what you get:

Fade-in of signal

  • $\begingroup$ this is what interpolation is suppose to do (see the interp1 command in the for loop )but as you can see from the two pictures at the beginning and the end it doesn't do a gradual spline $\endgroup$
    – Rick T
    Jan 31 '13 at 11:41
  • $\begingroup$ @RickT: I've changed my answer accordingly. $\endgroup$
    – Deve
    Jan 31 '13 at 12:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.