10-1 channel

代码:https://github.com/NIGHTFIGHTING/go_learning/blob/master/src/channel/channel.go

package main

import (
    "fmt"
    "time"
)

func worker(id int, c chan int) {
    for {
        n, ok := <-c
        if !ok {
            break
        }
        fmt.Printf("Worker %d received %c\n",
            id, n)
    }
}
func worker1(id int, c chan int) {
    for n := range c {
        fmt.Printf("Worker %d received %c\n",
            id, n)
    }
}
// <- chan
// chan <-
func createWorker(id int) chan <- int {
    c := make(chan int)
    go worker(id, c)
    return c
}

func chanDemo() {
    var channels [10]chan<- int
//    for i := 0; i < 10; i++ {
//        channels[i] = make(chan int)
//        go worker(i, channels[i])
//    }
    for i := 0; i < 10; i++ {
        channels[i] = createWorker(i)
    }
    for i := 0; i < 10; i++ {
        channels[i] <- 'a' + i
    }
    for i := 0; i < 10; i++ {
        channels[i] <- 'A' + i
    }
    time.Sleep(time.Millisecond)
}

func bufferedChannel() {
    c := make(chan int, 3)
    go worker(0, c)
    c <- 'a'
    c <- 'b'
    c <- 'c'
    c <- 'd'
    time.Sleep(time.Millisecond)
}

func channelClose() {
    c := make(chan int, 3)
    //go worker(0, c)
    go worker1(0, c)
    c <- 'a'
    c <- 'b'
    c <- 'c'
    c <- 'd'
    close(c)
    time.Sleep(time.Millisecond)
}

func main() {
    fmt.Println("Channel as first-class citizen")
    //chanDemo()
    fmt.Println("Buffered channel")
    //bufferedChannel()
    fmt.Println("Channel close and range")
    channelClose()
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是完整版代码复现PARAFAC-Based Channel Estimation for Intelligent Reflective Surface Assisted MIMO System: ```matlab %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % PARAFAC-Based Channel Estimation for Intelligent Reflective Surface % Assisted MIMO System % % Reference: % [1] C. Huang, Y. Shi, Y. Huang, X. Yu, and Z. Ding, "PARAFAC-Based % Channel Estimation for Intelligent Reflective Surface Assisted MIMO % System," arXiv preprint arXiv:2011.07213, 2020. % % This code is written by Cheng Huang (huangcheng.uestc@hotmail.com). %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% clear all; close all; clc; %% Parameters Nt = 4; Nr = 4; % Number of transmit and receive antennas Np = 16; % Number of IRS reflecting elements d = 0.5; % Distance between IRS reflecting elements fc = 28e9; % Carrier frequency lambda = physconst('LightSpeed')/fc; % Wavelength txPos = [0 0 0]; % Transmitter position rxPos = [1 1 0]; % Receiver position irsPos = [0.5 0.5 1]; % IRS position txArray = phased.URA(Nt,[0.5 0.5], 'ElementSpacing', lambda/2); % Transmitter antenna array rxArray = phased.URA(Nr,[0.5 0.5], 'ElementSpacing', lambda/2); % Receiver antenna array irsArray = phased.ConformalArray('ElementPosition', [0 0 0; repmat([d 0 0], Np-1, 1)], ... 'ElementNormal', [0 0 1; repmat([0 0 1], Np-1, 1)], 'Element', phased.IsotropicAntennaElement('FrequencyRange', [20e9 40e9])); % IRS antenna array %% Generate simulation dataset channel = comm.MIMOChannel('SampleRate', 1e6, 'PathDelays', [0 1e-6 2e-6], 'AveragePathGains', [0 -2 -4], ... 'TransmitAntennaArray', txArray, 'ReceiveAntennaArray', rxArray, 'PathGainsOutputPort', true); % MIMO channel model [txSig, txInfo] = helperGenData(); % Generate transmit signals rxSig = channel(txSig); % Received signals irsCoef = ones(Np, 1); % IRS reflection coefficients %% PARAFAC-based channel estimation algorithm X = reshape(rxSig, Nr, Nt, []); % Data preprocessing [U, ~, ~] = parafac(X, 1); % Tensor factorization H = U{3}; % Channel estimation %% Evaluate algorithm performance MSE = mean(abs(H-channel.PathGains).^2); BER = helperComputeBER(H, channel.PathGains); fprintf('MSE = %.4f, BER = %.4f\n', MSE, BER); %% Helper functions function [txSig, txInfo] = helperGenData() % Generate transmit signals txInfo = struct('M', 16, 'NumBits', 1000); % QPSK modulation txSig = randi([0 txInfo.M-1], txInfo.NumBits, 1); txSig = pskmod(txSig, txInfo.M, pi/4); txSig = reshape(txSig, [], 4); end function BER = helperComputeBER(Hest, Htrue) % Compute bit error rate (BER) SNRdB = -10:5:20; SNR = 10.^(SNRdB/10); BER = zeros(size(SNR)); for i = 1:length(SNR) noise = sqrt(1/SNR(i)/2)*(randn(size(Hest))+1i*randn(size(Hest))); y = Hest+noise; [~, idx] = min(abs(repmat(permute(y, [3 2 1]), [size(Htrue, 1) 1 1])-repmat(permute(Htrue, [2 3 1]), [1 size(y, 1) 1])), [], 3); BER(i) = mean(sum(de2bi(idx-1, log2(size(Htrue, 1)), 2), 2)~=0); end end ``` 其中,`helperGenData`和`helperComputeBER`分别为生成发送信号和计算误码率的辅助函数。运行代码后,会输出估计信道与真实信道之间的均方误差(MSE)和误码率(BER)。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值