Advertisement
Guest User

Untitled

a guest
Dec 7th, 2019
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.27 KB | None | 0 0
  1. %EE107: Final Project
  2. %Elsa Ames and Bennett Brain
  3.  
  4. %all figures are commented out for runtime, see finalproj_noloop to do
  5. %figures as it goes
  6.  
  7. %set these to pick equalizer and wave
  8.  
  9. wave = 1; %1 for SRRC, 0 for halfwave
  10. equa = 1; %1 for MMSE, 0 for zero-forcing
  11.  
  12. close all
  13. noisePow = .0001; %changeable parameter
  14. %% Image Pre-Processing
  15. image = imread('pomodoro.png');
  16. image = rgb2gray(image);
  17. image = im2double(image);
  18. [m, n] = size(image);
  19. fun = @dct2;
  20. dct_image = blkproc(image, [8 8], fun);
  21.  
  22. %Normalize Image
  23. zeroOffset = min(min(dct_image));
  24. dct_image = dct_image - zeroOffset;
  25. scaleFactor = max(max(dct_image));
  26. dct_image = dct_image / scaleFactor;
  27.  
  28.  
  29. %quantize to ints
  30. levels = linspace(0,1,255);
  31. B = imquantize(dct_image, levels);
  32. B = reshape(B,[8 8 (m*n/64)]);
  33.  
  34. if (wave == 0)
  35. hwav_B_recomp = zeros(size(B));
  36. else
  37. srrc_B_recomp = zeros(size(B));
  38. end
  39.  
  40. %% make SRRC and hwav pulses to start to improve runtime
  41. T = 1;
  42. t = linspace(0,1,33);
  43. A1 = 1; %amplitude
  44.  
  45.  
  46.  
  47. halfpulse = A1 * sin(pi*t/T);
  48.  
  49. %recalculate A1, halfpulse
  50. A1 = 1/sqrt(sum(halfpulse.^2)); %normalize power level
  51. halfpulse = A1*sin(pi*t/T);
  52. halfpulse = halfpulse(1:end-1);
  53.  
  54. a = .5;
  55. T = 1;
  56. K = 6;
  57.  
  58. b = rcosdesign(a, K*T, 32); %already is normalized
  59. b = b';
  60. % figure
  61. % plot(b)
  62. rcosL = length(b);
  63.  
  64. %% channel Part 1
  65.  
  66. hnpad = zeros(1,31);
  67. %initial
  68. hn = [1,hnpad,1/2,hnpad,3/4,hnpad,-2/7,hnpad];
  69.  
  70. %indoor
  71. %hn = [1,hnpad,.4365,hnpad,.1905,hnpad,.0832,hnpad,0,hnpad,.0158,hnpad,0,hnpad,.003,hnpad];
  72.  
  73. %outdoor
  74. %hn = [.5,hnpad,1,hnpad,0,hnpad,.63,hnpad,zeros(1,32*4),.25,hnpad,zeros(1,32*3),.16,hnpad,zeros(1,32*12),.1,hnpad];
  75.  
  76. Hejw = fft(hn,128*64);
  77.  
  78. %% MMSE equalizer Part 1
  79.  
  80.  
  81. Eb = 1; %because all signals were normalized
  82. if (equa == 1)
  83. Qmmse = conj(Hejw)./(abs(Hejw).^2+sigma.^2/Eb);
  84. qnmmse = ifft(Qmmse);
  85. else
  86. %% Zero-Forcing equalizer part 1
  87. Qzf = 1./Hejw;
  88. qnzf = ifft(Qzf);
  89. end
  90.  
  91.  
  92. %%
  93.  
  94. %Regroup into N blocks and reshape
  95. N = 6;
  96.  
  97. %% START LOOP HERE
  98.  
  99. %Note, N must divide m*n/64 without remainder for this to work well
  100. % for our image, m*n/64 is 33750, 3 does divide it
  101. for z = 0:N:(m*n/64)-N
  102.  
  103. currentBlocks = B(:,:,z+1:z+N); %current block will be 8 x 8 x N
  104.  
  105. dct_col = reshape(currentBlocks, 1, []);
  106. dct_col_binary = de2bi(dct_col', 8); %this is our bitstream
  107. bitstream = reshape(dct_col_binary', 1, []); % 2.3 conversion to a bitstream
  108. %% Half wave
  109.  
  110. if (wave == 0)
  111. half_wave = zeros(32*length(bitstream),1);
  112.  
  113. for k = 1:length(bitstream)
  114. if(bitstream(k) == 0)
  115. half_wave(k*32-31:k*32) = -halfpulse;
  116. end
  117.  
  118. if(bitstream(k) == 1)
  119. half_wave(k*32-31:k*32) = halfpulse;
  120. end
  121. end
  122.  
  123. %{
  124. figure
  125. plot(t(1:end-1), halfpulse)
  126.  
  127. figure
  128. plot(half_wave)
  129. xlim([1, 32*10]);
  130. %}
  131.  
  132. else
  133.  
  134. %% SRRC
  135.  
  136. bitstream2 = bitstream*2-1;
  137. bitstreampad = padarray(bitstream2,31,'post');
  138. bitstreampad = reshape(bitstreampad,1,[]);
  139.  
  140. rais_cos = conv(bitstreampad,b);
  141.  
  142. %{
  143. %testing signals
  144. figure
  145. plot(rais_cos)
  146. xlim([1,600])
  147. hold on
  148. plot([32,32],[-.05,.2],'b')
  149. plot([32*2,32*2],[-.05,.2], 'b')
  150. plot([32*3,32*3],[-.05,.2], 'b')
  151. plot([32*4,32*4],[-.05,.2], 'b')
  152. plot([32*5,32*5],[-.05,.2], 'b')
  153. plot([32*6,32*6],[-.05,.2], 'b')
  154. plot([32*7,32*7],[-.05,.2], 'b')
  155. plot([32*8,32*8],[-.05,.2], 'b')
  156. plot([32*9,32*9],[-.05,.2], 'b')
  157. hold off
  158. %}
  159.  
  160. %{
  161. eyediagram(half_wave,32,1,16)
  162. title('eye diagram half wave mod')
  163. eyediagram(rais_cos,32)
  164. title('eye diagram srrc mod')
  165. %}
  166.  
  167. end
  168.  
  169. %% channel
  170.  
  171. %hnpad = zeros(1,31);
  172. %hn = [1,hnpad,1/2,hnpad,3/4,hnpad,-2/7,hnpad];
  173.  
  174. %{
  175. figure
  176. freqz(hn); %this might be wrong
  177. %}
  178.  
  179. if(wave == 0)
  180. halfwav_channel = conv(half_wave,hn);
  181. else
  182. srrc_channel = conv(rais_cos,hn);
  183. end
  184.  
  185. %{
  186. figure
  187. plot(srrc_channel)
  188. xlim([1,300])
  189.  
  190. eyediagram(halfwav_channel,32,1,16)
  191. eyediagram(srrc_channel,32)
  192. %}
  193.  
  194. %% adding noise
  195.  
  196. sigma = sqrt(noisePow);
  197. if(wave == 0)
  198. noisemat_hwav = sigma*randn(size(halfwav_channel));
  199. hwav_noise = halfwav_channel + noisemat_hwav;
  200. else
  201. noisemat_srrc = sigma*randn(size(srrc_channel));
  202. srrc_noise = srrc_channel + noisemat_srrc;
  203. end
  204.  
  205. %{
  206. eyediagram(hwav_noise,32,1,16)
  207. eyediagram(srrc_noise,32)
  208. %}
  209.  
  210. %%
  211. %Matched filtering
  212.  
  213. if(wave == 0)
  214. hwav_match = flip(halfpulse);
  215. hwav_mfilt = conv(hwav_match,hwav_noise);
  216. else
  217. srrc_match = flip(b);
  218. srrc_mfilt = conv(srrc_match,srrc_noise);
  219. end
  220.  
  221. %{
  222. figure
  223. plot(hwav_match)
  224. title('Impulse response half wave matched filter')
  225. figure
  226. freqz(hwav_match)
  227. title('Freq response half wave matched filter')
  228.  
  229. figure
  230. plot(srrc_match)
  231. title('Impulse response srrc matched filter')
  232. figure
  233. freqz(srrc_match)
  234. title('Freq response srrc matched filter')
  235. %}
  236.  
  237.  
  238.  
  239. %{
  240. eyediagram(hwav_mfilt,32)
  241. eyediagram(srrc_mfilt,32)
  242. %}
  243.  
  244. %% Zero-Forcing equalizer
  245. % (almost) entirely commented out because MMSE equalizer is used instead
  246. %see other code for results of ZF equalizer
  247.  
  248. %{
  249. figure
  250. freqz(qnzf)
  251. %}
  252. if(equa == 0)
  253. if(wave == 0)
  254. hwav_equa = conv(qnzf,hwav_mfilt);
  255. else
  256. srrc_equa = conv(qnzf,srrc_mfilt);
  257. end
  258.  
  259. %{
  260. eyediagram(hwav_eq,32)
  261. eyediagram(srrc_eq,32)
  262. %}
  263.  
  264. else
  265.  
  266. %% MMSE equalizer
  267.  
  268. %Eb = 1; %because all signals were normalized
  269.  
  270. %Qmmse = conj(Hejw)./(abs(Hejw).^2+sigma.^2/Eb);
  271.  
  272. %qnmmse = ifft(Qmmse);
  273.  
  274. if(wave == 0)
  275. hwav_equa = conv(qnmmse,hwav_mfilt);
  276. else
  277. srrc_equa = conv(qnmmse,srrc_mfilt);
  278. end
  279.  
  280.  
  281. end
  282. %{
  283. eyediagram(hwav_mmse,32)
  284. eyediagram(srrc_mmse,32)
  285. %}
  286.  
  287. %% Sampling & Detection
  288.  
  289. if(wave == 0)
  290.  
  291. hwav_samplStart = 32;
  292. hwav_samplRate = 32;
  293. hwav_bitRecomp = zeros(size(bitstream));
  294. for p = 0:length(bitstream)-1
  295. if (hwav_equa(hwav_samplStart+p*hwav_samplRate) < 0)
  296. hwav_bitRecomp(p+1) = 0;
  297. else
  298. hwav_bitRecomp(p+1) = 1;
  299. end
  300. end
  301.  
  302. else
  303.  
  304. srrc_samplStart = 96+96;
  305. srrc_samplRate = 32;
  306. srrc_bitRecomp = zeros(size(bitstream));
  307.  
  308. for p = 0:length(bitstream)-1
  309. if (srrc_equa(srrc_samplStart+p*srrc_samplRate) < 0)
  310. srrc_bitRecomp(p+1) = 0;
  311. else
  312. srrc_bitRecomp(p+1) = 1;
  313. end
  314.  
  315. end
  316.  
  317. end
  318.  
  319. %% conversion back to image
  320.  
  321. if(wave == 0)
  322. hwav_binary_recomp = reshape(hwav_bitRecomp,8,[]);
  323. hwav_binary_recomp = hwav_binary_recomp';
  324. hwav_col_recomp = bi2de(hwav_binary_recomp);
  325. hwav_col_recomp = hwav_col_recomp';
  326. hwav_cblock_recomp = reshape(hwav_col_recomp,8,8,[]);
  327. hwav_B_recomp(:,:,z+1:z+N) = hwav_cblock_recomp;
  328.  
  329. else
  330. srrc_binary_recomp = reshape(srrc_bitRecomp,8,[]);
  331. srrc_binary_recomp = srrc_binary_recomp';
  332. srrc_col_recomp = bi2de(srrc_binary_recomp);
  333. srrc_col_recomp = srrc_col_recomp';
  334. srrc_cblock_recomp = reshape(srrc_col_recomp,8,8,[]);
  335. srrc_B_recomp(:,:,z+1:z+N) = srrc_cblock_recomp;
  336. end
  337.  
  338. %big for loop ends here
  339. end
  340.  
  341. fun2 = @idct2;
  342.  
  343. if(wave == 0)
  344. hwav_B_reshape = reshape(hwav_B_recomp, m,n);
  345. hwav_image_dct = rescale(hwav_B_reshape);
  346. hwav_image_dct = hwav_image_dct * scaleFactor;
  347. hwav_image_dct = hwav_image_dct + zeroOffset;
  348. hwav_image = blkproc(hwav_image_dct, [8,8], fun2);
  349. hwav_im = mat2gray(hwav_image);
  350. figure
  351. imshow(hwav_im)
  352.  
  353. else
  354. srrc_B_reshape = reshape(srrc_B_recomp, m,n);
  355. srrc_image_dct = rescale(srrc_B_reshape);
  356. srrc_image_dct = srrc_image_dct * scaleFactor;
  357. srrc_image_dct = srrc_image_dct + zeroOffset;
  358. srrc_image = blkproc(srrc_image_dct, [8,8], fun2);
  359. srrc_im = mat2gray(srrc_image);
  360. figure
  361. imshow(srrc_im)
  362. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement