tune_via_uart.c

  1. uint16_t arrTo1PulseWidth(volatile float arr[], int size, float targetWidth)
    {
        int iterator = 0, numBitWidths=0;
        uint16_t goodDataCount = 0, pass = 0;
        for(iterator=0;iterator<size;iterator++)
        {
    
            //
            // if the item is less than 10 times the bit width,
            //
            if(arr[iterator] < targetWidth*10)
            {
    
                //
                // if the item is not within +/-MARGINPERCENT% of the targetWidth,
                // then it is a multiple of 1-bit
                //
                bool belowBound = arr[iterator] < targetWidth*(1.0-MARGINPERCENT);
                bool aboveBound = arr[iterator] > targetWidth*(1.0+MARGINPERCENT);
                if(belowBound || aboveBound)
                {
    
                    //
                    // estimate how many bit-widths this is
                    //
                    numBitWidths = (int)((arr[iterator]/targetWidth)+0.5);
    
                    //
                    // multiply the multi-bit baudrate value by the estimated
                    // number of bits to make this a 1-bit baud estimate
                    //
                    arr[iterator] = arr[iterator]/numBitWidths;
    
                    //
                    // find if this new value is within the bounds
                    //
                    belowBound = arr[iterator] < targetWidth*(1.0-MARGINPERCENT);
                    aboveBound = arr[iterator] > targetWidth*(1.0+MARGINPERCENT);
                    if(belowBound || aboveBound)
                    {
                        arr[iterator] = 0; // discard if not within margins
                    }
                    else
                    {
                        goodDataCount++; // iterate good data counter
                    }
                }
                else
                {
    
                    //
                    // this is a 1-bit value so increment the counter for it
                    //
                    goodDataCount++;
                }
            }
            else
            {
                arr[iterator] = 0;
            }
        }
    
        //
        // if at least MINSAMPLEPERCENT% of the sampled values
        // are "good" samples, then return a pass
        //
        if((float)goodDataCount/(float)size > MINSAMPLEPERCENT)
        {
            pass=1;
        }
        return(pass); //return if the array had enough good samples or not
    }

            用于将一个浮点数数组转换为1脉冲宽度。函数的输入参数包括一个浮点数数组、数组大小和目标宽度。函数的输出是一个16位无符号整数,表示数组中是否有足够的“好”样本。函数的具体实现如下:

    1. 遍历数组中的每个元素,如果元素小于目标宽度的10倍,则进行下一步处理,否则将该元素设置为0。
    2. 如果元素不在目标宽度的±MARGINPERCENT范围内,则将其视为多个1位的波特率值,并将其乘以估计的位数,以使其成为1位波特率估计值。
    3. 如果新值仍然不在目标宽度的±MARGINPERCENT范围内,则将其设置为0,否则将其视为“好”数据并将“好”数据计数器加1。
    4. 如果“好”数据计数器占数组大小的比例大于MINSAMPLEPERCENT,则返回1,否则返回0。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
这段代码中包含了模型的微调(fine-tuning)部分。根据代码中的条件`args.fine_tune`,如果为`True`,则执行以下操作: 1. 修改模型的全连接层(fc): - `model.fc = nn.Linear(model.fc.in_features, args.classes_level2)`:将模型的全连接层修改为输出维度为`args.classes_level2`的线性层。这个操作可能是为了在微调时,将模型的输出层调整为新的分类任务。 2. 加载预训练模型权重: - `model.load_state_dict(...)`:从指定路径加载预训练模型的权重。`config.save_path`是保存模型权重的路径,`config.model_name`是模型的名称,`name`是根据`config.classify_type`生成的新名称,`5`是一个数字,可能表示预训练模型的版本号或其他标识符。这个操作可能是为了将预训练模型的权重加载到模型中,以便在微调过程中使用。 3. 冻结预训练模型的参数: - `for param in model.parameters(): param.requires_grad = False`:将模型中所有参数的梯度计算设置为不可求导,即冻结参数。这个操作可能是为了在微调过程中只更新新添加的全连接层的参数。 4. 修改模型的全连接层(fc)为新的分类任务: - `model.fc = nn.Linear(model.fc.in_features, config.num_classes)`:将模型的全连接层修改为输出维度为`config.num_classes`的线性层。这个操作可能是为了适应新的分类任务,将模型的输出层调整为正确的类别数。 5. 使用 Xavier 初始化方法和常数初始化方法对新的全连接层参数进行初始化: - `torch.nn.init.xavier_normal_(model.fc.weight.data)`:使用 Xavier 初始化方法对全连接层的权重进行初始化。 - `nn.init.constant_(model.fc.bias.data, 0)`:使用常数初始化方法将全连接层的偏置项初始化为零。 通过以上操作,可以实现对预训练模型的微调,将其适应新的分类任务。需要注意的是,这段代码中的具体逻辑和参数值可能根据实际情况有所变化,你可以根据实际的代码逻辑和配置参数来理解这段代码的具体作用。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值