Advertisement
mbazs

Sinusoid, envelope, freq sweep

Feb 24th, 2020
2,115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Octave 1.60 KB | None | 0 0
  1. clear;
  2. warning off;
  3.  
  4. %
  5. % A sinusoid base signal (optionally with a gaussian envelope) and a freq sweep in a dot product plot
  6. %
  7. % multiply base signal with gaussian bell or not
  8. gaussian_envelope = false
  9.  
  10. % sample rate, time base
  11. srate = 500;
  12. time  = -1:1/srate:1-1/srate;
  13.  
  14. % base signal
  15. freq = 5;
  16. phase = 0*pi/4;
  17. base = sin(2 * pi * freq * time + phase);
  18. % multiplied by gaussian envelope
  19. if gaussian_envelope
  20.   envelope = exp((-time .** 2) / .1);
  21. else
  22.   envelope = ones(1, length(time)); % identity
  23. end
  24. signal = base .* envelope;
  25.  
  26. % plot base signal (and gaussian)
  27. subplot(211);
  28. hold on, grid on
  29. plot(time, envelope, 'g', 'linew', 2);
  30. plot(time, signal, 'k', 'linew', 3);
  31. hold off;
  32. title('Signal with envelope');
  33. % not required because of autoscale later
  34. % set(gca, 'xlim', [-1 1]);
  35.  
  36. % dot product with signal and frequency sweep
  37. sweep_freqs = 2:.2:8; % 2Hz to 8Hz with step of .2Hz
  38. dot_prods = zeros(length(sweep_freqs));
  39. for sweep_freq_index = 1:length(sweep_freqs)
  40.   sweep_signal = sin(2 * pi * sweep_freqs(sweep_freq_index) * time);
  41.   dot_prods(sweep_freq_index) = dot(sweep_signal, signal); % / length(time);
  42. endfor
  43.  
  44. % stem plot for dot prod / sweep frequencies
  45. subplot(212);
  46. hold on, grid on
  47. stem(sweep_freqs, dot_prods, 'k', 'linew', 3, 'markersize', 10, 'markerfacecolor', 'b');
  48. hold off
  49. title('Dot products by sweep frequency');
  50. % not required because of autoscale later
  51. % set(gca, 'ylim', [sweep_freqs(1)-.5 sweep_freqs(end)+.5], 'ylim', [-2 2]);
  52.  
  53. % autoscale now
  54. axis("auto");
  55.  
  56. % print('dot_prod_w_gaussian_envelope.svg', '-dsvg', '-S1280,1024');
  57. waitforbuttonpress();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement