findlambda.m 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. function lambda = findlambda(X,numspikes,fs)
  2. %FINDLAMBDA get thresholding parameter for specified number of spikes
  3. %
  4. % Input parameters:
  5. % X : SocialSparsity neighborhood
  6. % numspikes : Number of spikes
  7. % fs : Sampling frequency
  8. %
  9. %
  10. % Description:
  11. % findlamda(X,numspikes,fs) finds the lambda value for which we get
  12. % the specified number of spikes by softthresholding. We start by
  13. % finding a lambda which is gives more than numspikes and a lambda
  14. % which gives less than numspikes and bisect the interval until we
  15. % get the correct number of spikes.
  16. % Note: This is very slow...
  17. %
  18. % Dependencies:
  19. % getSpikePositions
  20. bl = max(X(:));
  21. bu = max(X(:));
  22. curr_numspikes = 0;
  23. counter = 0;
  24. while curr_numspikes < numspikes
  25. bl = bu;
  26. bu = bu / 2;
  27. curr_data = max(0,1-(bu./X));
  28. curr_data = sum(abs(curr_data),2);
  29. [~,curr_numspikes] = getSpikePositions(curr_data,fs);
  30. b = bu;
  31. end
  32. while (curr_numspikes ~= numspikes)
  33. b = (bl + bu)/2;
  34. curr_data = max(0,1-(b./X));
  35. curr_data = sum(abs(curr_data),2);
  36. [~,curr_numspikes] = getSpikePositions(curr_data,fs);
  37. if (curr_numspikes > numspikes)
  38. bu = b;
  39. else
  40. bl = b;
  41. end
  42. end
  43. lambda = b;