# Sparse Signal fitting in MATLAB, for a sinusoidal function with more than 8 terms?

I'm trying to fit some data belong to a sum of sines function (Fourier sparse) in MATLAB, however, the number of terms of sine function in MATLAB is limited,i.e. to $1 \leq n \leq 8$. However, I want more terms in my fit functions, i.e. over $50$ terms. By the way I do not intent to recover the signal using Compressive-sensing sparse recovery methods or Fourier based curve fitting and my goal is not signal estimation.

• Is there anyway to make MATLAB to fit my data to a sum of sine function with over 8 sinusoidal terms?
• Why there is such constraint in MATLAB (is it technically or arbitrary)? - Is there any toolbox to fit sinusoidal function (especially something that is capable of supporting wieghted data)?
f = fit(X,Y, 'sin10')
Error using fittype>iCreateFromLibrary (line 412)
Library function sin10 not found.

It is o.k up to sin8 or sin9 parameters.

• Does each additional term have an increase infrequency proportional to the fundamental frequency (such that you are basically finding the Fourier series)? Because then each sine fit should be independent of each other, because these sine functions are orthogonal to each other. Aug 9 '16 at 2:33
• No, actually the frequencies are randomly chosen. I can't employ fourier, because of leakage. Aug 9 '16 at 11:42
• Can you please explain a little bit more why you cannot use the Fourier Transform to analyse a function into a sum of sinusoids, especially when the result might be sparse? What sort of discriminating capability are you going for? What is the shortest distance (in Hz) you expect two frequencies to end up with? Also, can you say a little bit more about the actual problem you are dealing with?
– A_A
Aug 10 '16 at 10:25
• @A_A , first, the problem with FFT is leakage (to avoid leakage I must know exactly the frequencies to make window length is the most appropriate) , other than leakage, I have a big problem. Some points in my samples have greater priority than other (like 20% of data are more accurate measurements of the signal). Through curve fitting I can easily wight these important data more. – Mimsaad 38 mins ago Aug 10 '16 at 11:39

MATLAB has a curve-fitting toolbox where you pass a function of your choice to the fit function. Hence, the number of terms is totally up to you in this case.

If you do not have a curve-fitting toolbox, you could try to find a free one or write a fit function by yourself (e.g. with fminsearch). However, this is not the fastest algorithm and also you would have to calculate the fitting errors by yourself.

FitOptions = fitoptions('Method','NonlinearLeastSquares', 'Algorithm', 'Trust-Region', 'MaxIter');
FitType = fittype('a*sin(1*f) + b*sin(2*f) + c*sin(3*f) + d*sin(4*f) + e*sin(5*f) + g*sin(6*f) + h*sin(7*f) + k*sin(8*f) + l*sin(9*f) + m*sin(10*f) + n*sin(11*f)', 'independent', 'f');
[FittedModel, GOF] = fit(freq, data, FitType)


will certainly fit the sum of sines with the prefactors. And, of course, scaling parameters in the individual sine's arguments. However, since I do not have MATLAB available here, I cannot verify this. But from my experience, fit() has no such restriction. The restriction you experience comes most likely from the limitations of an internal function library that you are using (see the "sin10" string in your example - this is a call to an built-in, predefined function.)

• Thanks, however I am aware of that toolbox, as I said it does not support fitting sum of sines with more than 8 term. Aug 9 '16 at 11:48
• See the updated answer, please. You are not using the fit() function with its full flexibility, I assume.
– M529
Aug 9 '16 at 17:07
• Honestly, I was just testing this method,and in first shop it did not bring the results I desire (however, the same test data when I use 'sin8' parameters give good results). I think this method is right and I only must adjust Initiation values of the algorithm properly. I''ll check it and let you know. Aug 9 '16 at 17:16
• "(see the "sin10" string in your example - this is a call to an built-in, predefined function.) " And I wonder why they restrict to only 8 sinusoidal terms? Is the algorithm the culprit? Aug 9 '16 at 17:17
• No, it is not the algorithm. It is the fact that you cannot provide an infinite number of built-in functions. At a certain level of detail or complexity, you have to type in your own function. There is no sense in trying to provide functions for each use case.
– M529
Aug 9 '16 at 17:45