Advertisement
Guest User

Untitled

a guest
May 11th, 2018
189
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MatLab 6.94 KB | None | 0 0
  1. %ALPHA 0.6 FOR RGB PWM REACTIVE LIGHTING : AS IF ACTUALLY SAMPLING
  2. %Joe Swelland 5/11/2018
  3. %==========================================================================
  4. %ALPHA 0.5 : Introduction of Peak Detection
  5. %Import Audio
  6. [clip, Fs] = audioread('hose5.wav'); %Import test song
  7. clipt = clip(:,1);
  8.  
  9. %=======================================================================
  10. %FILTERING
  11. %-----------------------------------------------------------------------
  12. %controls
  13. lowfc = 150;
  14. midfc = 600;
  15. highfc = midfc;
  16. intensity = 1; %background intensity
  17.  
  18. %COLOR ASSIGNMENTS (RANDOMLY CHOSEN)
  19. %LOW SPECTRUM - BLUE
  20. [lowA, lowB] = butter(4,lowfc/(Fs/2));
  21. %MID SPECTRUM - GREEN
  22. [midA, midB] = butter(4,[lowfc/(Fs/2) midfc/(Fs/2)]);
  23. %HIGH SPECTRUM - RED
  24. [highA, highB] = butter(4,highfc/(Fs/2), 'high');
  25.  
  26. %SIGNAL FILTERING
  27. %LOW
  28. lowFl = intensity * filter(lowA,lowB,clipt);
  29. %MID
  30. midFl = intensity * filter(midA,midB,clipt);
  31. %HIGH
  32. highFl = intensity * filter(highA,highB,clipt);
  33.  
  34. %======================================================================
  35. %UPDATE FRAMES : to hold value of color for X duration
  36. %----------------------------------------------------------------------
  37. %controls
  38. cnt = 0; %iterator
  39. interval = 0.05; %time in seconds for color duration
  40. win = interval * Fs;
  41.  
  42. %LOW
  43. blueFrame = zeros(length(clipt),1);
  44. maxVolume = 0;
  45. for i=1:length(clipt)
  46.     volume = abs(lowFl(i));
  47.     if (cnt > win)
  48.         maxVolume = volume;
  49.         cnt = 0;
  50.     else
  51.         cnt = cnt + 1; %increment window counter
  52.         if (volume > maxVolume)
  53.             maxVolume = volume;
  54.             cnt = 0; %reset window counter
  55.         end
  56.     end
  57.     blueFrame(i) = maxVolume;
  58. end
  59. %MID
  60. greenFrame = zeros(length(clipt),1);
  61. maxVolume = 0;
  62. for i=1:length(clipt)
  63.     volume = abs(midFl(i));
  64.     if (cnt > win)
  65.         maxVolume = volume;
  66.         cnt = 0;
  67.     else
  68.         cnt = cnt + 1; %increment window counter
  69.         if (volume > maxVolume)
  70.             maxVolume = volume;
  71.             cnt = 0; %reset window counter
  72.         end
  73.     end
  74.     greenFrame(i) = maxVolume;
  75. end
  76. %HIGH
  77. redFrame = zeros(length(clipt),1);
  78. maxVolume = 0;
  79. for i=1:length(clipt)
  80.     volume = abs(highFl(i));
  81.     if (cnt > win)
  82.         maxVolume = volume;
  83.         cnt = 0;
  84.     else
  85.         cnt = cnt + 1; %increment window counter
  86.         if (volume > maxVolume)
  87.             maxVolume = volume;
  88.             cnt = 0; %reset window counter
  89.         end
  90.     end    
  91.     redFrame(i) = maxVolume;
  92. end
  93.  
  94. %==========================================================================
  95. %BUMP DETECTOR
  96. %--------------------------------------------------------------------------
  97. %controls
  98. volume = 0;
  99. avgTot = 0;
  100. avgLog = zeros((floor(0.07*Fs)),1);
  101. currAvg = zeros(length(clipt),1);
  102. logSlope = zeros(length(clipt),1);
  103. a = 1; %iterator
  104. lastAvg = 0;
  105. up = 1;
  106. peak = 0;
  107. n = 0; %iterator
  108. %lowAvgLog=zeros(length(clipt),1);
  109.  
  110.  
  111. for i=1:length(clipt)
  112.     volume = abs(clipt(i)); %grab current sample  
  113.     if (a > (floor(0.07 * Fs))) %average array rollover
  114.         a = 1;
  115.         avgTot = avgTot - avgLog(a);
  116.         avgLog(a) = volume;
  117.         avgTot = avgTot + avgLog(a);
  118.     else %non-rollover
  119.         avgTot = avgTot - avgLog(a);
  120.         avgLog(a) = volume;
  121.         avgTot = avgTot + avgLog(a);
  122.         a = a + 1;
  123.     end  
  124.    %Update average
  125.    currAvg(i) = avgTot/(0.07*Fs);
  126.    %Slope Detection of Average
  127.    if (up) %Upwards mode
  128.        if (currAvg(i) > lastAvg)
  129.            lastAvg = currAvg(i);
  130.            n = 0;
  131.        elseif (currAvg(i) < lastAvg)
  132.            if (n >(0.5*Fs)) %will only switch to down if no new max in entire window length n
  133.              %peak detected, follow down next
  134.              if (abs(currAvg(i) - lastAvg) > lastAvg/4) %if the change is tiny, probably not a big enough slope change
  135.                up = 0;
  136.                peak = 0; %sound peak detected toggle
  137.              end
  138.              n = 0; %reset window counter
  139.            else
  140.               n = n + 1; %increment window counter
  141.            end
  142.        end
  143.    elseif (up==0) %Downwards mode, works mostly same as upwards.
  144.        if (currAvg(i) < lastAvg)
  145.            lastAvg = currAvg(i);
  146.            n = 0;
  147.        elseif (currAvg(i) > lastAvg)
  148.            if (n > (0.5*Fs))
  149.                %trough detected, follow up next
  150.                if (abs(currAvg(i) - lastAvg) > currAvg(i)/4)
  151.                     up = 1;
  152.                     peak = 1;
  153.                end
  154.                n = 0;
  155.            else
  156.                n = n + 1;
  157.            end
  158.        end
  159.    end  
  160.    %if currently in a sound bump
  161.    if (peak)
  162.        logSlope(i) = 1 - fade; %decay intensity of bump
  163.        fade = fade + logSlope(i)/10000; %decay rate, made it up.
  164.    else
  165.        logSlope(i) = 0; %if not bumping, bump effect = 0
  166.        fade = 0;
  167.    end  
  168. end
  169.  
  170. %==========================================================================
  171. %ADD BUMP TO FRAMES : still under develeopment. not sure how I want to have
  172. %a bump effect color.
  173. %--------------------------------------------------------------------------
  174. for i=1:length(clipt)
  175.     if (blueFrame(i) > redFrame(i) && blueFrame(i) > greenFrame(i) && logSlope(i))
  176.         greenFrame(i) = redFrame(i) + logSlope(i);
  177.         %redFrame(i) = greenFrame(i) + logSlope(i);
  178.         %blueFrame(i) = blueFrame(i);
  179.     elseif (redFrame(i) > blueFrame(i) && redFrame(i) > greenFrame(i) && logSlope(i))
  180.         %greenFrame(i) = greenFrame(i) + logSlope(i);
  181.         blueFrame(i) = blueFrame(i) + logSlope(i);
  182.         %redFrame(i) = redFrame(i);
  183.     elseif (greenFrame(i) > blueFrame(i) && greenFrame(i) > redFrame(i) && logSlope(i))
  184.         redFrame(i) = redFrame(i) + logSlope(i);
  185.         %blueFrame(i) = blueFrame(i) + logSlope(i);
  186.         %greenFrame(i) = greenFrame(i);
  187.     end
  188. end
  189.  
  190. %==========================================================================
  191. %SIMULATION OF LIGHTS : draws a "square" frame of color representing the
  192. %RGB output to the LED
  193. %--------------------------------------------------------------------------
  194. %controls
  195. spacing = 5;%width in pixels of each color frame
  196. draw_start = 0; %seconds
  197. draw_time = 120; %seconds
  198. current = 1; %image array index for colors
  199. total_samples = draw_time * Fs; %total samples expected to be drawn.
  200. squares = total_samples/(interval*Fs); %amount of squares of color to draw. Squares SHOULD be length = interval seconds
  201. next = current + spacing; %jump pixels to next color
  202. move = 0; %reseting just incase
  203. drw_smple_start = draw_start*Fs; %image sample start
  204. stream=zeros(500, floor(squares), 3); %create image stream
  205.  
  206. for i=1:floor(squares)
  207.     move = move + (interval*Fs);
  208.    
  209.     %SQUARE CREATION
  210.     stream(:,current:next,1)= redFrame(drw_smple_start+move); %RED
  211.     stream(:,current:next,2)= greenFrame(drw_smple_start+move); %GREEN
  212.     stream(:,current:next,3)= blueFrame(drw_smple_start+move); %BLUE
  213.     %Move to next box
  214.     current = current + spacing;
  215.     next = next + spacing;
  216. end
  217.  
  218. figure(1)
  219. imshow(stream)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement